Jump to content
  • Advertisement
Sign in to follow this  
Serapth

[.net] GDI+ Image question

This topic is 4354 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all. Quick question, is there a way using GDI+ to copy and paste a System.Drawing.Bitmap into another Bitmap? I can make a copy of the source image, but there doesnt seem to be a "Paste into" type method. I can replace on image with anothers data, but that really isnt what I am trying to accomplish. There is the option of doing LockBits() on the source and destination image, but I want to keep the code as readable as possible. This is also the reason I want to strictly use GDI+ calls ( no DX, Managed DX, etc... ) Right now, im doing it bit by bit, like this: newImage.SetPixel(i+currentX,j,curImg.GetPixel(i, j)); Let me tell you... its SLOW.... It works, but its pretty painful. Please, someone, tell me im missing a simple Method here somewhere.

Share this post


Link to post
Share on other sites
Advertisement
You should use a Graphics object and the DrawImage method. For example;
Bitmap Source = new Bitmap("source.png");
Bitmap Destination = new Bitmap(100, 100);
Graphics g = Graphics.FromImage(Destination);
g.DrawImage(Source, 10, 20); // Draw source onto destination at coordinates (10, 20)

Note that because of the way drawing (and pixel offsets) work, you might want to set g.PixelOffsetMode = PixelOffsetMode.Half;. Otherwise, the image will be drawn half a pixel higher and to the left than you might have expected, resulting in a blurry result. You should set this before the DrawImage call.

Share this post


Link to post
Share on other sites
Doh!

Argh, thats it exactly, thanks so much. Biggest downside to large frameworks is finding what you need. I should have known this from way to many WinForm programs.

Thanks again, apps about 10x faster now :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!