使用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# DataGridView控件清空数据完美解决方法

    C# DataGridView控件绑定数据后清空数据在清除DataGridview的数据时: 1.DataSource为NULL(DataGridView.DataSource= null;)这样会将 ...

  2. db2 重启

    DB2重启数据库实例时,有时停止实例会失败,此时需要先确认没有应用链接数据库,然后再关闭数据库实例,并重新启动. 1.查看是否有活动的链接 命令:db2 list applications for d ...

  3. 修改Arduino串口缓冲区大小(转)

    本帖节选自<Arduino程序设计基础>第二版5.1.6串口缓冲区       在之前的示例程序中,我们都是采用人工输入测试数据的方式检验程序效果,Arduino每接收到一次数据,就会将数 ...

  4. The Zero

    TOday is just a alpha, but there isnot 欧米伽. 编程,是你选的,是你学的,也是你喜欢的,更是你追求的.为什么要放弃塔!. 加油! 为自己. #include&l ...

  5. javascript按中文首字母排序

    resultValue=[ '武汉' , '北京' , '上海' , '天津' ] ; resultValue= resultValue.sort( function compareFunction( ...

  6. winAPI 检查系统设备拔插使用 WM_DEVICECHANGE 消息

    if(message->message == WM_DEVICECHANGE) { /*if (message->wParam == DBT_DEVICEARRIVAL || messag ...

  7. NGUI BUG /各种坑

    以下为:NGUI3.6具体使用过程中碰到的 各种BUG /各种坑 备忘 1.UIToggle 使用UIToggle组件实现多个tab标签互斥,如果是在代码中动态的设置显示某个tab标签对应的内容,则需 ...

  8. 【总结】清除webbrowser cookie/session的6种方法

    下面是我测试下来的6种清除webbrowser中cookie的6种方法: //方法一:调用 wininet.dll清除cookie (推荐) SuppressWininetBehavior(); // ...

  9. javascript json转为 go struct 小工具代码

    /** * Created by cdpmac on 15/10/20. */ var topname="Ap"; var jdata={ "item": { ...

  10. android 使用LinearGradient进行字体渐变的效果

    有这么一种效果,一串字符有一束白光从字体上面闪光的效果.如下图显示: 就像上面的显示效果一样一束白光闪过,这种效果主要还是使用了LinearGradient类来进行的 LinearGradient也称 ...