我使用这个代码来捕捉屏幕:C#捕获屏幕为8位(256色)位图
public Bitmap CaptureWindow(IntPtr handle)
{
// get te hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// get the size
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
// create a device context we can copy to
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
// bitblt over
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest, hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
// get a .NET image object for it
Bitmap img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap);
return img;
}
然后我想将位图转换为256色(8位)。我想这个代码,但获取有关不能够从一个索引位图格式创建图像的错误:
Bitmap img8bit = new Bitmap(img.Width,img.Height,
System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
Graphics g = Graphics.FromImage(img8bit);
g.DrawImage(img,new Point(0,0));
我确实看到了一些例子来转换不同格式之间的位图,但对我来说我在寻找从屏幕捕获时执行此操作的最佳方法。例如,如果有一种方法可以更好地工作,即创建一个8位位图,然后将光标移动到该位置,则优先于Caputring屏幕首先对位图进行兼容,然后对其进行转换。除非最好捕捉然后转换。
我有一个使用Borland Builder 6.0 VCL编写的使用C++编写的程序,而且我正在尝试memic。在这种情况下,为VCL的TBitmap对象设置像素格式很简单。我注意到Bitmap.PixelFormat在.NET中是只读的,呃。
更新:在我来说,我不认为答案是复杂的,需要找出最佳的调色板项,因为Graphics.GetHalftonePalette使用屏幕一些其他使用DC应该是很好的,因为我原来的位图来自屏幕,而不仅仅是任何可能来自文件/电子邮件/下载/等的随机位图。我相信有20行代码涉及到DIB和GetHalftonePalette可以完成,但目前还找不到它。
你总是会被捕捉整个屏幕?如果是这样,那么有一种内置的方法可以用.NET来实现这一点,即'Graphics.CopyFromScreen'。 – Ryan 2012-04-17 04:41:17
'PixelFormat'属性是只读的,但是有一个Bitmap构造函数可以让你指定像素格式。请参阅http://msdn.microsoft.com/en-us/library/3z132tat.aspx。 – 2012-04-17 04:45:44
@eselk你有没有进一步与此? :) – 2013-04-09 22:34:39