Can't StretchBlt an image loaded from my hard drive

D

D. Yates

I can't seem to figure out why I can't StretchBlt an image that I have
loaded from my hard drive. There are two button events below. The first
button event, btnFormStretch_Click, works fine, but the second one,
btnStretch_Click, does not. The only difference seems to be the way that I
get the source graphic object. The first method uses the forms graphic
object whereas the second uses the Graphics.FromImage method. Any ideas?

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
public static extern bool StretchBlt(IntPtr hdcDest,
int nXOriginDest, // x-coord of destination upper-left corner
int nYOriginDest, // y-coord of destination upper-left corner
int nWidthDest, // width of destination rectangle
int nHeightDest, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXOriginSrc, // x-coord of source upper-left corner
int nYOriginSrc, // y-coord of source upper-left corner
int nWidthSrc, // width of source rectangle
int nHeightSrc, // height of source rectangle
System.Int32 dwRop // raster operation code
);



private void btnFormStretch_Click(object sender, System.EventArgs e)
{
Graphics gSource = this.CreateGraphics();
IntPtr dcSource = gSource.GetHdc();

Image bmpDest = new Bitmap(this.ClientRectangle.Width,
this.ClientRectangle.Height);
Graphics gDest = Graphics.FromImage(bmpDest);
IntPtr dcDest = gDest.GetHdc();

StretchBlt(dcDest, 0, 0, bmpDest.Width, bmpDest.Height,
dcSource, 0, 0, bmpDest.Width - 20, bmpDest.Height - 20, 0xCC0020);

gSource.ReleaseHdc(dcSource);
gDest.ReleaseHdc(dcDest);
bmpDest.Save(@"D:\Dave\TestImageCapture\Captured.bmp", ImageFormat.Bmp);
MessageBox.Show("Finished Saving Image");
}

private void btnStretch_Click(object sender, System.EventArgs e)
{
Bitmap bmpSource = new Bitmap(@"D:\Dave\TestImageCapture\Test2.bmp"); //
simple 16x16 bitmap
Graphics gSource = Graphics.FromImage(bmpSource);

Bitmap bmpDest = new Bitmap(32, 32); // double the size
Graphics gDest = Graphics.FromImage(bmpDest);
IntPtr dcSource = gSource.GetHdc();
IntPtr dcDest = gDest.GetHdc();

StretchBlt(dcDest, 0, 0, bmpDest.Width ,bmpDest.Height, dcSource, 0, 0,
bmpSource.Width, bmpSource.Height, 0xCC0020);

gSource.ReleaseHdc(dcSource);
gDest.ReleaseHdc(dcDest);

bmpDest.Save(@"D:\Dave\TestImageCapture\Captured.bmp", ImageFormat.Bmp);
MessageBox.Show("Finished Saving Image");
}


Thanks,
Dave
 
D

D. Yates

My going to post this in microsoft.public.dotnet.framework.drawing since
nobody seems to know the answer here.
 

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

Top