在c#中使用bitblt显示图片
使用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
);
}
}
在c#中使用bitblt显示图片的更多相关文章
- Bootstrap中data-src无法显示图片,但是src可以
在学习bootstrap时,书中的源码是用的data-src来定义图像位置,但是我在使用的时候无法显示图片:data-src可以在img标签中使用来显示图片吗?我使用src可以,而是用data-src ...
- Android4.4中WebView无法显示图片解决方案
在Android4.4之前我们在使用WebView时为了提高加载速度我设置了(myWebView.getSettings().setBlockNetworkImage(true);//图片加载放在最后 ...
- 七、在U-boot中让LCD显示图片
1. 增加Nandflash读取代码 因为要显示图片,而图片明显是放在Nandflash中比较合适,因此需要有能够操作Nandflash的函数.在U-boot中已经有能操作Nandflash的函数了, ...
- Android中高效的显示图片之一 ——加载大图
在网上看了不少文章,发现还是官方文档介绍最详细,把重要的东西简单摘要出来.详细可看官方文档地址 ( http://www.bangchui.org/read.php?tid=9 ) . 在应用中显示图 ...
- Android中高效的显示图片之三——缓存图片
加载一张图片到UI相对比较简单,如果一次要加载一组图片,就会变得麻烦很多.像ListView,GridView,ViewPager等控件,需要显示的图片和将要显示的图片数量可能会很大. 为了减少内存使 ...
- javaweb中上传图片并显示图片,用我要上传课程信息(里面包括照片)这个例子说明
原理: 从客户端上传到服务器 照片——文件夹——数据库 例如:桌面一张照片,在tomacat里创建upload文件夹,把桌面照片上传到upload文件夹里,并且把照片的 ...
- Android中高效的显示图片之二——在非UI线程中处理图片
在“加载大图”文章中提到的BitmapFactory.decode*方法,如果源数据是在磁盘.网络或其它任何不是在内存中的位置,那么它都不应该在UI线程中执行.因为它的加载时间不可预测且依赖于一系列因 ...
- OpenGL3.x,4.x中使用FreeImage显示图片的BUG-黑色,或颜色分量顺序错乱
//参照FreeImage官网给出的CTextrueManager写的加载函数 //官方给的例子是用opengl3.0以下的旧GL写的,没有使用glGenerateMipmap(GL_TEXTURE_ ...
- ionic ng-src 在网页显示,但是导出apk在android手机中运行不显示图片
解决方法参照: http://stackoverflow.com/questions/29896158/load-image-using-ng-src-in-android-ionic-aplicat ...
随机推荐
- 有关STL 标准模板库
1.vector 本质:对数组的封装 特点:读取能在常数时间内完成
- String、StringBuffer、StringBuilder的区别
在日常开发过程中String字符串估计是被用到最多的变量了,最近看了一些String.StringBuffer和StringBuilder的东西,三者都可以对字符串进行操作,他们究竟有什么区别,以及适 ...
- 3.3.2 pulseIn(pin,state,timeout)
pulseIn函数用于读取引脚脉冲的时间长度,脉冲可以是HIGH或LOW.如果是HIGH,函数将先等引脚变为高电平,然后开始计时,一直到变为低电平为止.返回脉冲持续的时间长短, 单位为ms.如果超时还 ...
- arduino编程语言Wiring参考手册API
对Arduino的编程是利用 Arduino编程语言 (基于 Wiring)和Arduino开发环境(based on Processing)来实现的. 通过编程,Arduino可以实现很多种功能. ...
- install LLVM
version >= 3.8.0 $ cd llvm... $ mv someofClang ./tools $ mkdir build $ cd build $ cmake -DCMAKE_B ...
- C#设置字体(FontDIalog)、颜色(ColorDialog)对话框控件
设置字体控件为FontDialog,设置颜色的控件为ColorDialog.这两个控件的使用和OpenFileDialog(打开文件)及FolderBroswerDialog(打开文件夹)的使用类似. ...
- 利用node构建本地服务
利用node构建本地服务 首先安装下node.js,地址为https://nodejs.org/en/,然后安装npm. node.js的中文api地址http://nodeapi.ucdok.com ...
- swoole 使用 1
在很长的一段时间里,我不太看好swoole,发现它的文档太少,社区也不够活跃等,但是最近在学习 Hprose时,发现swoole在rpc方面做得更加完善,于是决定看看. 在简单的使用swoole扩展后 ...
- 如此清除sql server 2008 记住的用户名
在C盘下搜索这个文件 SqlStudio.bin 搜索到后删除就可以,这样登录Sql server 时就不会有用户名密码之类的,相当于做了初始化操作,但里面的数据库什么的都是有的. 这只是个删除记住 ...
- REDIS源码中一些值得学习的技术细节02
Redis中散列函数的实现: Redis针对整数key和字符串key,采用了不同的散列函数 对于整数key,redis使用了 Thomas Wang的 32 bit Mix Function,实现了d ...