Hello all,
I've got some trouble woth using BitBlt in C#. Basically what I would like to achieve is to BitBlt a Bitmap to the Panel Background, replacing all older graphics previously on the Panel Background.
To acheive without BitBlting I can do:
Graphics g = this.CreateGraphics();
g.Clear(Color.White);
g.DrawImageUnscaled(baseBitmap, 0, 0);
baseBitmap is a BitmapImage I created earlier. This works but there's flickering. Plus I would like something faster. The only alternative would be BitBLT.
Now, If I try BitBlt like this:
Graphics g = Graphics.FromImage(baseBitmapt);
Graphics g2 = this.CreateGraphics();
Bitmap targetBMP = new Bitmap(this.Width, this.Height,g);
Graphics targetG = Graphics.FromImage(targetBMP);
IntPtr targetHDC = targetG.GetHdc();
IntPtr gHDC = g.GetHdc();
IntPtr gHDC2 = g2.GetHdc();
//Copy baseBitmap to targetBMP
BitBlt(targetHDC, 0, 0, this.Width, this.Height, gHDC, 0, 0, 13369376);
//Copy targetBMP to the Panel Background Graphics
BitBlt(gHDC2, 0, 0, this.Width, this.Height, targetHDC, 0, 0, 13369376);
targetG.ReleaseHdc(targetHDC);
g.ReleaseHdc(gHDC);
g2.ReleaseHdc(gHDC2);
All I get as a result is a blank black screen as the Panel Background Image.
Can anyone help me with this please? I tried looking all over the web, but all samples are in C++ and are slightly complex. Plus I'm just looking for something simple so that I can do this quickly.
Any help will be appreciated.
Thanks
Regards
Nathanael
Loading
christian de wetPosted Aug 22, 2007, 8:49 AM
//Below I want to overlay the selectedGeometriesBitmap over baseBitmap and BitBlt this to //the Panel Graphics.
Graphics sdcGraphics = this.CreateGraphics();
IntPtr sdc = sdcGraphics.GetHdc();
IntPtr sourceHBMP = baseBitmap.GetHbitmap();
IntPtr overlayHBMP = selectedGeometriesBitmap.GetHbitmap();
IntPtr sourceDC = CreateCompatibleDC(sdc);
SelectObject(sourceDC, sourceHBMP);
IntPtr hdc = CreateCompatibleDC(sdc);
SelectObject(hdc, overlayHBMP);
BitBlt(sourceDC, 0, 0, this.Width, this.Height, hdc, 0, 0, (int)TernaryRasterOperations.SRCAND);
BitBlt(sdc, 0, 0, this.Width, this.Height, sourceDC, 0, 0, (int)TernaryRasterOperations.SRCCOPY);
DeleteDC(sourceDC);
DeleteDC(hdc);
ReleaseDC(IntPtr.Zero, sdc);
DeleteObject(sourceHBMP);
DeleteObject(overlayHBMP);
selectedGeometriesBitmap.Dispose();
When I do the above, the colors are distored.
Any ideas?
Thanks
Regards,
Nathanael
christian de wetPosted Aug 22, 2007, 8:39 AM
What I did was :
IntPtr sourceHBMP = baseBitmap.GetHbitmap();
Graphics sdcGraphics = this.CreateGraphics();
IntPtr sdc = sdcGraphics.GetHdc();
IntPtr hdc = CreateCompatibleDC(sdc);
SelectObject(hdc, sourceHBMP);
BitBlt(sdc, 0, 0, this.Width, this.Height, hdc, 0, 0, 13369376);
DeleteDC(hdc);
ReleaseDC(IntPtr.Zero, sdc);
DeleteObject(sourceHBMP);
So it seems that one needs to create an HBitmap from each bitmap you want to BitBlt. But now, if I do this, the colors are distorted. When BitBlitting the HBitmaps on each other the final image has the result I want but all with the wrong colors. So it seems that the colors get distorted when converting to an HBitmap. Which is not completely acceptable in a GIS commercial app.
Has someone a solution to this?
Thanks for the help
Regards,
Nathanael