[.net] GDI+ Image question
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.
You should use a Graphics object and the DrawImage method. For example;
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement