具有索引像素格式的图形运行时错误 [英] Graphics with indexed pixel format run time error
问题描述
好的所以我有两个摄像头,我的软件正在使用。第一个是电脑摄像头。当我连接并运行此代码时:
Bitmap currentFrame = videoSourcePlayer.GetCurrentVideoFrame();
Graphics g = Graphics.FromImage(currentFrame);
g.DrawImage(currentFrame, videoSourcePlayer.imagex, videoSourcePlayer.imagey, videoSourcePlayer.imgWidth, videoSourcePlayer.imgHeight);
pictureBox1.Image = currentFrame;
return currentFrame;
网络摄像头正常工作。我可以复制我的缩放和平移,就好了。
the webcam works fine. I am able to copy my zoom and pan, just fine.
但是当我连接到另一个cam并运行相同的代码时,我收到此错误: HResult = 0x80131500消息=无法从具有索引像素格式的图像创建图形对象。 Source = System.Drawing StackTrace:at System.Drawing.Graphics.FromImage(Image
image)
but when I connect to the other cam and run the same code, I get this error : HResult=0x80131500 Message=A Graphics object cannot be created from an image that has an indexed pixel format. Source=System.Drawing StackTrace: at System.Drawing.Graphics.FromImage(Image image)
所以我尝试了这段代码:
so I tried this code :
private Bitmap CaptureScreen()
{
Bitmap originalBmp = videoSourcePlayer.GetCurrentVideoFrame();
// Create a blank bitmap with the same dimensions
Bitmap tempBitmap = new Bitmap(originalBmp.Width, originalBmp.Height);
// From this bitmap, the graphics can be obtained, because it has the right PixelFormat
using (Graphics g = Graphics.FromImage(tempBitmap))
{
g.DrawImage(originalBmp, videoSourcePlayer.imagex, videoSourcePlayer.imagey, videoSourcePlayer.imgWidth, videoSourcePlayer.imgHeight);
pictureBox1.Image = originalBmp;
return originalBmp;
}
}
代码修复运行时错误,我得到但是摆脱我的变焦和平移。我不知道为什么?但我确实有一个缩放和平移功能,适用于第一个代码块但不适用于第二个。
that code fix the runtime error, I get but is get rid of my zoom and pan. I do not know why? but I do have a zoom and pan function that works with the 1st code block but not with the second.
如何修复A Graphics对象不能从具有索引像素格式运行时错误的图像创建,但保持我的缩放和平移?
how can I fix the A Graphics object cannot be created from an image that has an indexed pixel format runtime error but keep my zoom and pan?
推荐答案
也许在第二种情况下你应该写:
<跨度>&NBSP;&NBSP; pictureBox1.Image = tempBitmap;
pictureBox1.Image = tempBitmap;
返回tempBitmap;
return tempBitmap;
这篇关于具有索引像素格式的图形运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!