使用 StretchBlt 生成的缩略图是全黑?
请教一下,下面的方法,截屏,然后生成缩略图是正确的:
IntPtr handle = ImageHelper.GDI32.GetDesktopWindow();//获得屏幕的HWND
IntPtr sourceDC = User32.GetWindowDC(handle);//获得屏幕DC
而换成图片
//Graphics sourceGraphics = Graphics.FromImage(bmp);//使用源图创建画板
//IntPtr sourceDC = sourceGraphics.GetHdc(); //得到画板的DC
则会得到全黑的缩略图,实在找不到错在哪里,请指教
---------------------------------------------------------
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="bmp">源图</param>
/// <param name="widthDest">缩略图宽度</param>
/// <param name="heightDest">缩略图高度</param>
/// <returns></returns>
public static Bitmap MakeThubnail(Bitmap bmp, int widthDest, int heightDest)
{
try
{
//Graphics sourceGraphics = Graphics.FromImage(bmp);//使用源图创建画板
//IntPtr sourceDC = sourceGraphics.GetHdc(); //得到画板的DC
IntPtr handle = ImageHelper.GDI32.GetDesktopWindow();//获得屏幕的HWND
IntPtr sourceDC = User32.GetWindowDC(handle);//获得屏幕DC
// create a device context we can copy to
IntPtr destCDC = GDI32.CreateCompatibleDC(sourceDC); //建立与sourceDC兼容的DC,用于输出
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(sourceDC, widthDest, heightDest);//再建立一个与hdcSrc兼容的位图(双缓冲?)
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(destCDC, hBitmap); //将DC与位图关联
GDI32.SetStretchBltMode(destCDC, GDI32.STRETCH_HALFTONE);
GDI32.POINTAPI point;
GDI32.SetBrushOrgEx(destCDC, 0, 0, out point);
// bitblt over
GDI32.StretchBlt(destCDC, 0, 0, widthDest, heightDest, sourceDC, 0, 0, bmp.Width,bmp.Height, GDI32.SRCCOPY); //复制
// restore selection
GDI32.SelectObject(destCDC, hOld); //取消与位图的关联
// clean up
GDI32.DeleteDC(destCDC);
//User32.ReleaseDC(handle, sourceDC);
//sourceGraphics.ReleaseHdc();
// get a .NET image object for it
Bitmap img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap);
return img;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}