使用bitblt比DrawImage有更好的性能

using AForge.Video.DirectShow;
using System;
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); //默认启动双缓冲
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
#endif
var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
videoSource = new VideoCaptureDevice(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 TernaryRasterOperations : uint
{
SRCCOPY = 0x00CC0020,
SRCPAINT = 0x00EE0086,
SRCAND = 0x008800C6,
SRCINVERT = 0x00660046,
SRCERASE = 0x00440328,
NOTSRCCOPY = 0x00330008,
NOTSRCERASE = 0x001100A6,
MERGECOPY = 0x00C000CA,
MERGEPAINT = 0x00BB0226,
PATCOPY = 0x00F00021,
PATPAINT = 0x00FB0A09,
PATINVERT = 0x005A0049,
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 dwRop // raster operation code
); [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr obj); [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
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 nSrcWidth,
int nSrcHeight,
int dwRop
);
}
}

参考:experts-exchange.com

在c#中使用bitblt显示图片的更多相关文章

  1. Bootstrap中data-src无法显示图片,但是src可以

    在学习bootstrap时,书中的源码是用的data-src来定义图像位置,但是我在使用的时候无法显示图片:data-src可以在img标签中使用来显示图片吗?我使用src可以,而是用data-src ...

  2. Android4.4中WebView无法显示图片解决方案

    在Android4.4之前我们在使用WebView时为了提高加载速度我设置了(myWebView.getSettings().setBlockNetworkImage(true);//图片加载放在最后 ...

  3. 七、在U-boot中让LCD显示图片

    1. 增加Nandflash读取代码 因为要显示图片,而图片明显是放在Nandflash中比较合适,因此需要有能够操作Nandflash的函数.在U-boot中已经有能操作Nandflash的函数了, ...

  4. Android中高效的显示图片之一 ——加载大图

    在网上看了不少文章,发现还是官方文档介绍最详细,把重要的东西简单摘要出来.详细可看官方文档地址 ( http://www.bangchui.org/read.php?tid=9 ) . 在应用中显示图 ...

  5. Android中高效的显示图片之三——缓存图片

    加载一张图片到UI相对比较简单,如果一次要加载一组图片,就会变得麻烦很多.像ListView,GridView,ViewPager等控件,需要显示的图片和将要显示的图片数量可能会很大. 为了减少内存使 ...

  6. javaweb中上传图片并显示图片,用我要上传课程信息(里面包括照片)这个例子说明

    原理:  从客户端上传到服务器                照片——文件夹——数据库 例如:桌面一张照片,在tomacat里创建upload文件夹,把桌面照片上传到upload文件夹里,并且把照片的 ...

  7. Android中高效的显示图片之二——在非UI线程中处理图片

    在“加载大图”文章中提到的BitmapFactory.decode*方法,如果源数据是在磁盘.网络或其它任何不是在内存中的位置,那么它都不应该在UI线程中执行.因为它的加载时间不可预测且依赖于一系列因 ...

  8. OpenGL3.x,4.x中使用FreeImage显示图片的BUG-黑色,或颜色分量顺序错乱

    //参照FreeImage官网给出的CTextrueManager写的加载函数 //官方给的例子是用opengl3.0以下的旧GL写的,没有使用glGenerateMipmap(GL_TEXTURE_ ...

  9. ionic ng-src 在网页显示,但是导出apk在android手机中运行不显示图片

    解决方法参照: http://stackoverflow.com/questions/29896158/load-image-using-ng-src-in-android-ionic-aplicat ...

随机推荐

  1. C. Shaass and Lights 组合数学

    http://codeforces.com/contest/294/problem/C 把那个数组n分段了,那么有两类. 1.开头和端点那些,就是只有一端在开始的,这个时候,要开完这些灯,只能循序渐进 ...

  2. Markdown syntax guide and writing on MWeb

    Philosophy Markdown is intended to be as easy-to-read and easy-to-write as is feasible.Readability, ...

  3. ruby Matrix 输出 格式化

    require 'matrix' class Matrix def to_pretty_s s = "" i = 0 while i < self.column_size s ...

  4. linux批量复制或删除同命名规则的文件

    如图所示,有多个不同后缀的文件,但他们的前缀都是"QC_TZ.impute." 此时想批量复制图中的文件的话,可以考虑用命令行 cp QC_TZ.impute.* /your/de ...

  5. bootstrop验证手机号

    formValidate($form,getRules(),getMessages()); function getRules(){ return { "memberViewVo.membe ...

  6. [刘阳Java]_JVM工作流程_第4讲

    程序员写好一段Java源程序-->编译-->字节码-->JVM-->硬件平台(操作系统)

  7. 在CMMI推广过程中EPG常犯的错误(转)

    本文转自: http://developer.51cto.com/art/200807/86953.htm 仅用于个人收藏,学习.如有转载,请联系原作者. ---------------------- ...

  8. Robot Framework入门学习2 创建第一个测试用例

    本文章部分内容引自以下网址,感谢作者的辛苦分享 http://www.cnblogs.com/fnng/p/3871712.html http://blog.csdn.net/tulituqi/art ...

  9. spring mvc(1):基础入门

      依赖 pom.xml ( maven ) <properties>  <spring.version>3.0.5.RELEASE</spring.version> ...

  10. linux修改主机名称

    http://blog.csdn.net/qq_20480611/article/details/51017033 ========================================== ...