首页 / C# / c#-将相机图像渲染到WPF图像控件
c#-将相机图像渲染到WPF图像控件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-将相机图像渲染到WPF图像控件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1955字,纯文字阅读大概需要3分钟。
内容图文
![c#-将相机图像渲染到WPF图像控件](/upload/InfoBanner/zyjiaocheng/673/3e4f4bc6415d462abeec9b95dd9c13c0.jpg)
我有一个uEye相机,我以1000ms的间隔拍摄图像的快照,我想像这样在WPF Image控件中进行渲染
Bitmap MyBitmap;
// get geometry of uEye image buffer
int width = 0, height = 0, bitspp = 0, pitch = 0, bytespp = 0;
long imagesize = 0;
m_uEye.InquireImageMem(m_pCurMem, GetImageID(m_pCurMem), ref width, ref height, ref bitspp, ref pitch);
bytespp = (bitspp + 1) / 8;
imagesize = width * height * bytespp; // image size in bytes
// bulit a system bitmap
MyBitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
// fill the system bitmap with the image data from the uEye SDK buffer
BitmapData bd = MyBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
m_uEye.CopyImageMem(m_pCurMem, GetImageID(m_pCurMem), bd.Scan0);
MyBitmap.UnlockBits(bd);
我试图以1秒的速率将这些位图放入Image控件中.我怎样才能让Bitmap出现在Image控件中,并在我完成处理后立即处置它们,以使最小的内存占用量成为一个好的小程序员:)?
解决方法:
这是我们的操作方式(对我来说,它无需加载CPU(约5%)即可以200fps的速度工作):
private WriteableBitmap PrepareForRendering(VideoBuffer videoBuffer) {
PixelFormat pixelFormat;
if (videoBuffer.pixelFormat == PixFrmt.rgb24) {
pixelFormat = PixelFormats.Rgb24;
} else if (videoBuffer.pixelFormat == PixFrmt.bgra32) {
pixelFormat = PixelFormats.Bgra32;
} else if (videoBuffer.pixelFormat == PixFrmt.bgr24) {
pixelFormat = PixelFormats.Bgr24;
} else {
throw new Exception("unsupported pixel format");
}
var bitmap = new WriteableBitmap(
videoBuffer.width, videoBuffer.height,
96, 96,
pixelFormat, null
);
_imgVIew.Source = bitmap;
return bitmap;
}
private void DrawFrame(WriteableBitmap bitmap, VideoBuffer videoBuffer, double averangeFps) {
VerifyAccess();
if (isPaused) {
return;
}
bitmap.Lock();
try {
using (var ptr = videoBuffer.Lock()) {
bitmap.WritePixels(
new Int32Rect(0, 0, videoBuffer.width, videoBuffer.height),
ptr.value, videoBuffer.size, videoBuffer.stride,
0, 0
);
}
} finally {
bitmap.Unlock();
}
fpsCaption.Text = averangeFps.ToString("F1");
}
内容总结
以上是互联网集市为您收集整理的c#-将相机图像渲染到WPF图像控件全部内容,希望文章能够帮你解决c#-将相机图像渲染到WPF图像控件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。