[.net] GDI+ Image question

Started by
1 comment, last by Serapth 17 years, 7 months ago
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.
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.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

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 :)

This topic is closed to new replies.

Advertisement