开发者

C# picturebox drawing speed

I experienced that the I. method is slower then II. My application makes realtime image processing. I know C# is not ideal for this purpose, but I can separate the performance critical things and make it in C++ for example. I'm wondering why slower in C# a simple Bitmap drawing without any computation. My question: is there faster method in C# then I.?

I. C#:

void DrawImg(Bitmap b)
{
    pictureBox1.Image = b;
    pictureBox1.开发者_C百科Invalidate(); 
}

II. C# + C++ + OpenCV

C# side:
[DllImport("MyWrapper.dll")]
static extern void showimg(IntPtr p, int x, int y);

void DrawImg(Bitmap b)
{
        BitmapData d = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
        showimg(d.Scan0, d.Width, d.Height);
        b.UnlockBits(d);
}

C++ side:
extern "C" void __stdcall showimg(uchar* p, int x, int y)
{
    Mat img; // OpenCV's Mat class
    img.create(y, x, CV_8UC3);
    img.data = p;
    imshow( "Window1", img );
}


The PixelFormat of the image is critical. The vast majority of machines today run their video adapter in 32bpp mode. That makes the Format32bppPArgb more than 10 times faster than any other ones, including 32bppArgb and 24bppRgb. Do make sure to create the image in that format, convert it if necessary.

Also very crucial is that you draw the image with its original size. You do so in the native code but not necessarily with the PictureBox. It uses high quality interpolation on the image if it needs to resize the image. That takes time.


I'm not really sure what you're asking. Why is drawing with C# is slower than drawing with C++? Primarily because the standard Graphics class provided by the .NET Framework uses GDI+ drawing methods, which are far slower than the GDI functions used by C/C++.

Are you wondering how to speed up drawing in C#? It would be most helpful if you'd post the relevant section(s) of your code that you feel are too slow. While using the Graphics library is definitely slower than some of the alternatives, in the majority of cases, it's performance is not a factor. Unless you're drawing lots of graphics in a tight loop, you shouldn't experience any issues. If you are, it's more likely that your code can be optimized, rather than replaced entirely. (Of course, C# is not what you'll want to use if you are actually seeking real-time graphics. There aren't enough details in your question to tell.)

Alternatively, you can P/Invoke the GDI-based drawing functions from the Win32 API and/or consider using unsafe code in your C# application to manipulate bitmaps directly. This should give you very comparable performance to the drawing code in the C++ DLL you mention in your question, because you'd be using the same routines. However, once again, you'll need to provide more details in your question if any of us is to provide more specific, helpful advice.


PictureBox does a bit more background stuff than just drawing a image.

Try drawing directly onto a control/window in the paint event.

Graphics.DrawImageUnscaled(...) is probably going to be the fastest Framework method.

If you want faster then you need to look at DirectX or OpenGL and offload the drawing to dedicated hardware.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜