Bitblt provides better performance than drawimage
Using aforge. video. directShow; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. drawing. imaging; using system. LINQ; using system. runtime. interopservices; using system. text; using system. windows. forms; namespace Hongying. test {public partial class bitblttester: FORM {private videocapturedevice videosource; Public bitblttester () {Initializecomponent (); # If gdiplus setstyle (controlstyles. optimizeddoublebuffer, true); // double buffer setstyle (controlstyles. doublebuffer, true); setstyle (controlstyles. userpaint, true); setstyle (controlstyles. resizeredraw, true); setstyle (controlstyles. allpaintinginwmpaint, true); // do not erase the background. # endif var videodevices = new filterinfocollection (filtercategory. videoinputdevice); videosource = new videoc Apturedevice (videodevices [1]. monikerstring); videosource. newframe + = _ videosource_newframe; videosource. start ();} protected override void onpaint (painteventargs e) {base. onpaint (E); # If gdiplus if (frame! = NULL) {e. graphics. drawimage (frame, clientrectangle) ;}# else # endif} public Enum labels: uint {srccopy = 0x00cc0020, srcpaint = 0x00ee0086, srcand = 0x008800c6, SRCINVERT = 0x00660046, srcerase = 0x00440328, notsrccopy = 0x00330008, NOTSRCERASE = 0x001100a6, mergecopy = 0x00c000ca, mergepaint = 0x00bb0226, patcopy = 0x00f00021, patpaint = signature, patinvert = signature, dstinvert = 0x00550009, blackness = 0x00000042, whiteness = 0x00ff0062, captureblt = 0x40000000 // only if winver> = 5.0.0 (see wingdi. h)} bitmap frame; private const int srccopy = 0xcc0020; void _ videosource_newframe (Object sender, aforge. video. newframeeventargs eventargs) {frame = (Bitmap) eventargs. frame. clone (); # If gdiplus invalidate (); # else bitmap sourcebitmap = frame; graphics sourcegraphics = graphics. fromimage (FRAME); graphics destgraphics = creategraphics (); intptr destdc = destgraphics. gethdc (); intptr destcdc = createcompatibledc (destdc); intptr olddest = SelectObject (destcdc, intptr. zero); intptr sourcedc = sourcegraphics. gethdc (); intptr sourcecdc = createcompatibledc (sourcedc); intptr sourcehb = sourcebitmap. gethbitmap (); intptr oldsource = SelectObject (sourcecdc, sourcehb); int success = stretchblt (destdc, 0, 0, width, height, sourcecdc, 0, 0, sourcebitmap. width, sourcebitmap. height, (INT) ternaryrasteroperations. srccopy); SelectObject (destcdc, olddest); SelectObject (sourcecdc, oldsource); deleteobject (destcdc); deleteobject (sourcecdc); deleteobject (sourcehb); destgraphics. releasehdc (); sourcegraphics. releasehdc (); # endif eventargs. frame. dispose ();} [system. runtime. interopservices. dllimportattribute ("gdi32.dll")] public static extern intptr createcompatibledc (intptr HDC); [system. runtime. interopservices. dllimport ("gdi32.dll")] Private Static extern int bitblt (intptr hdcdest, // handle to destination DC (device context) int nxdest, // X-coord of destination upper-left corner int nydest, // y-coord of destination upper-left corner int nwidth, // width of destination rectangle int nheight, // height of destination rectangle intptr hdcsrc, // handle to source DC int nxsrc, // X-coordinate of source upper-left corner int nysrc, // y-coordinate of source upper-left corner system. int32 dwdrop // raster operation code); [system. runtime. interopservices. dllimportattribute ("gdi32.dll")] public static extern intptr SelectObject (intptr HDC, intptr OBJ); [system. runtime. interopservices. dllimportattribute ("Courier")] public static extern void deleteobject (intptr OBJ); [dllimport ("GDI32", entrypoint = "stretchblt")] public static extern int stretchblt (intptr HDC, int X, int y, int nwidth, int nheight, intptr hsrcdc, int xsrc, int ysrc, int nscwidth, int nscheight, int dwdrop );}}
Reference: experts-exchange.com
Use bitblt to display images in C #