使用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. 修改Shp文件名称

    IWorkspaceFactory factory = new ShapefileWorkspaceFactoryClass(); IWorkspace pworkspace = factory.Op ...

  2. shift

    -------siwuxie095 shift 更改批处理文件中可替换参数的位置 语法: SHIFT [/n] 如果命令扩展被启用,SHIFT 命令支持 /n 命令行开关:该命令行开关告诉 命令从第 ...

  3. setValue和setObject的区别

    在NSMutableDictionary的方法中有setValue forKey与setObject forKey,它们都可以用来设置某一个key值对应的value 1,setValue: forKe ...

  4. 细说;(function ($, undefined){ })(jQuery); 的使用

    1. 对于function前面的分号(;)的使用:使用分号的目的是为了防止多个文件压缩合并时,以为其他文件最后一行语句没加分号,而引起合并后的语法错误. 2. (function ($, undefi ...

  5. Oracle归档模式和非归档模式

    一 什么是Oracle归档模式? Oracle数据库有联机重做日志,这个日志是记录对数据库所做的修改,比如插入,删除,更新数据等,对这些操作都会记录在联机重做日志里.一般数据库至少要有2个联机重做日志 ...

  6. FIFO

    FIFO存储器 FIFO是英文First In First Out 的缩写,是一种先进先出的数据缓存器,他与普通存储器的区别是没有外部读写地址线,这样使用起来非常简单,但缺点就是只能顺序写入数据,顺序 ...

  7. <<Design Patterns>> Gang of Four

    One:Introduction: One-1:Before  delving into the  some twenty pattern designs, it's necessary for ME ...

  8. AndroidLinker与SO加壳技术之下篇

    点此查看上篇<AndroidLinker与SO加壳技术之上篇> 2.4 链接 链接过程由 soinfo_link_image 函数完成,主要可以分为四个主要步骤: 1. 定位 dynami ...

  9. phpStudy 的Apache虚拟主机配置

    放弃了wamp,朋友介绍了phpstudy,不错的一款软件,关键是能自由切换php版本.相关的阿帕奇虚拟主机配置参考:http://www.th7.cn/system/win/201506/10846 ...

  10. shell学习--grep2

    grep相关的练习,解释下面grep表达式的含义: grep '\<Tom\>' file 打印file中包含单词 Tom的行 grep 'Tome Savage' file 打印file ...