Graphics

D

DaveL

hello
I have a Bit map 1367 wide 32 high
this bitmap contains like 40 separate Images
32x32
I tell it the id*32 to get the approiate Image from
the source Bitmap


When i CreateGraphics() From the Standard CreateGraphics() function the code
below works
perfect..
when i CreateGraphics.FromImage()
the Resuting Bitmap is Not Copied Right.... i think. when i assign the
bitmap to a picturebox or button its not correct


can anybody tell me what the problem is here

int Id=10;
Rectangle Rect = new Rectangle(new Point(Id*32, 0), new Size(32,
32));
Bitmap bmp = new Bitmap(Rect.Width, Rect.Height);

//Graphics g = Graphics.FromImage(bmp); // draws to
bitmap but is all messed up
Graphics g = CreateGraphics(); //
works perfect with this but draws to the window at 0,0 i want a bitmap back
g.DrawImage(Source, 0, 0, Rect, GraphicsUnit.Pixel);
g.Dispose();


Thanks DaveL
 
J

Jeff Johnson

DaveL said:
hello
I have a Bit map 1367 wide 32 high
this bitmap contains like 40 separate Images
32x32
I tell it the id*32 to get the approiate Image from
the source Bitmap


When i CreateGraphics() From the Standard CreateGraphics() function the
code below works
perfect..
when i CreateGraphics.FromImage()
the Resuting Bitmap is Not Copied Right.... i think. when i assign the
bitmap to a picturebox or button its not correct


can anybody tell me what the problem is here

int Id=10;
Rectangle Rect = new Rectangle(new Point(Id*32, 0), new Size(32,
32));
Bitmap bmp = new Bitmap(Rect.Width, Rect.Height);

//Graphics g = Graphics.FromImage(bmp); // draws to
bitmap but is all messed up
Graphics g = CreateGraphics(); //
works perfect with this but draws to the window at 0,0 i want a bitmap
back
g.DrawImage(Source, 0, 0, Rect, GraphicsUnit.Pixel);
g.Dispose();

You might want to repost in microsoft.public.dotnet.framework.drawing.
However, where in that posted code are you using the bmp variable? The only
place I see it is in the stuff you commented out. And where do you want to
draw the bitmap to? You'll need two Graphics objects: one for the bitmap
(the source) and one for the destination.
 
D

DaveL

New bmp is created 32x32
CreateGraphics() draws to the form perfect
when i CreateGraphics.FromImage(bmp) // new bmp
then i use g.drawImage(Source < source bmp, 0,0,Rect,GraphicsUnit.Pixel);
// draws to new bmp but when i place bmp on control not correct


that is all below
 
L

Lee

This function returns a bitmap that is a sub-section of the original

If you look at your code and this function you will see where your
thought process went awry.

Remember that if you create a graphics object from a bitmap, then
anything done to the graphics object is applied to your bitmap, so
after your g.DrawImage, all you needed to do was use your altered
bitmap.

I created a dummy 1367x32 image then an altered version of your code,
I itterated through id from 0 to 39 and set a picturebox image
property to the resulting bitmap. All looked fine.

The function below was found at: http://msdn.microsoft.com/en-us/library/aa457087.aspx

public Bitmap Copy(Bitmap srcBitmap, Rectangle section)
{
// Create the new bitmap and associated graphics object
Bitmap bmp = new Bitmap(section.Width, section.Height);
Graphics g = Graphics.FromImage(bmp);

// Draw the specified section of the source bitmap to the
new one
g.DrawImage(srcBitmap, 0, 0, section, GraphicsUnit.Pixel);

// Clean up
g.Dispose();

// Return the bitmap
return bmp;
}
}

L. Lee Saunders
http://oldschooldotnet.blogspot.com
 
J

Jeff Johnson

Lee, I am using my alterd bitmap
below is new code from that link you posted
i still get the same results, if i draw to screen perfect
if i draw to bitmap not perfect


private void button3_Click(object sender, EventArgs e)
{
Bitmap srcBitmap =
(Bitmap)Bitmap.FromFile(@"D:\_netdev\dev\pics\band32.bmp");
Rectangle section = new Rectangle(16 * 32, 0, 32, 32);
// Create the new bitmap and associated graphics object
Bitmap bmp = new Bitmap(section.Width, section.Height);
Graphics g = Graphics.FromImage(bmp);

// Draw the specified section of the source bitmap to the new
one
g.DrawImage(srcBitmap, 0, 0, section, GraphicsUnit.Pixel);

// Clean up
g.Dispose();
this.button3.Image = bmp;

You probably need to set the PixelFormat of the Bitmap to the proper value.
You may be losing resolution.

But so far you've said "it's wrong" and it's "not perfect." This is not
giving us much information on what's REALLY wrong.
 
L

Lee

Your image is in a lower DPI (76) than screen dpi (96). It is an
issue with Bitmaps. If you saved it as a PNG, it would look fine.
Otherwise, sections to the right and bottom are getting cut off.

I changed the DPI of the image to 96 and now it works just fine.

L. Lee Saunders
http://oldschooldotnet.blogspot.com
 
D

DaveL

when graphics draws to the screen
its correct 32x32 same as in the original bitmap
just the 32x32 portion

when i use graphics created from the new bitmap
i get somewhat the correct area of the original bitmap
but its not correct
I really dont know who to explain this problem

one way works fine , the other does not work correctly

Dave
 
D

DaveL

Lee
I was wondering about that,
I did look into the resolution, but did
not know how to change it

Just like to thank you very much
for your time...

DaveP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top