【发布时间】:2009-05-21 14:42:39
【问题描述】:
因此,我一直在我的网站上就上述主题标题分享一些关于快速、不安全的像素访问的想法。一位先生给了我一个粗略的例子,说明他如何在 C++ 中做到这一点,但这对我在 C# 中没有帮助,除非我可以互操作,而且互操作也很快。我在互联网上找到了一个使用 MSDN 帮助编写的类,用于不安全地访问像素。上课速度特别快,但还不够快。这是课程:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace DCOMProductions.Desktop.ScreenViewer {
public unsafe class UnsafeBitmap {
Bitmap bitmap;
// three elements used for MakeGreyUnsafe
int width;
BitmapData bitmapData = null;
Byte* pBase = null;
public UnsafeBitmap(Bitmap bitmap) {
this.bitmap = new Bitmap(bitmap);
}
public UnsafeBitmap(int width, int height) {
this.bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
}
public void Dispose() {
bitmap.Dispose();
}
public Bitmap Bitmap {
get {
return (bitmap);
}
}
private Point PixelSize {
get {
GraphicsUnit unit = GraphicsUnit.Pixel;
RectangleF bounds = bitmap.GetBounds(ref unit);
return new Point((int)bounds.Width, (int)bounds.Height);
}
}
public void LockBitmap() {
GraphicsUnit unit = GraphicsUnit.Pixel;
RectangleF boundsF = bitmap.GetBounds(ref unit);
Rectangle bounds = new Rectangle((int)boundsF.X,
(int)boundsF.Y,
(int)boundsF.Width,
(int)boundsF.Height);
// Figure out the number of bytes in a row
// This is rounded up to be a multiple of 4
// bytes, since a scan line in an image must always be a multiple of 4 bytes
// in length.
width = (int)boundsF.Width * sizeof(Pixel);
if (width % 4 != 0) {
width = 4 * (width / 4 + 1);
}
bitmapData =
bitmap.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
pBase = (Byte*)bitmapData.Scan0.ToPointer();
}
public Pixel GetPixel(int x, int y) {
Pixel returnValue = *PixelAt(x, y);
return returnValue;
}
public void SetPixel(int x, int y, Pixel colour) {
Pixel* pixel = PixelAt(x, y);
*pixel = colour;
}
public void UnlockBitmap() {
bitmap.UnlockBits(bitmapData);
bitmapData = null;
pBase = null;
}
public Pixel* PixelAt(int x, int y) {
return (Pixel*)(pBase + y * width + x * sizeof(Pixel));
}
}
}
基本上我正在做的是复制整个屏幕并将每个像素与旧副本进行比较。在 1680x1050 位图上,使用以下代码大约需要 300 毫秒。
private Bitmap GetInvalidFrame(Bitmap frame) {
Stopwatch sp = new Stopwatch();
sp.Start();
if (m_FrameBackBuffer == null) {
return frame;
}
Int32 pixelsToRead = frame.Width * frame.Height;
Int32 x = 0, y = 0;
UnsafeBitmap unsafeBitmap = new UnsafeBitmap(frame);
UnsafeBitmap unsafeBuffBitmap = new UnsafeBitmap(m_FrameBackBuffer);
UnsafeBitmap retVal = new UnsafeBitmap(frame.Width, frame.Height);
unsafeBitmap.LockBitmap();
unsafeBuffBitmap.LockBitmap();
retVal.LockBitmap();
do {
for (x = 0; x < frame.Width; x++) {
Pixel newPixel = unsafeBitmap.GetPixel(x, y);
Pixel oldPixel = unsafeBuffBitmap.GetPixel(x, y);
if (newPixel.Alpha != oldPixel.Alpha || newPixel.Red != oldPixel.Red || newPixel.Green != oldPixel.Green || newPixel.Blue != oldPixel.Blue) {
retVal.SetPixel(x, y, newPixel);
}
else {
// Skip pixel
}
}
y++;
} while (y != frame.Height);
unsafeBitmap.UnlockBitmap();
unsafeBuffBitmap.UnlockBitmap();
retVal.UnlockBitmap();
sp.Stop();
System.Diagnostics.Debug.WriteLine(sp.Elapsed.Milliseconds.ToString());
sp.Reset();
return retVal.Bitmap;
}
是否有任何可能的方法/手段/方法可以将其加速到大约 30 毫秒?我可以使用 Graphics.CopyFromScreen() 在大约 30 毫秒内复制屏幕,从而每秒产生大约 30 帧。但是,一个程序的运行速度与其较慢的对应程序一样快,因此 GetInvalidFrame 中的 300 毫秒延迟将其减慢到每秒大约 1 - 3 帧。这不适合会议软件。
任何朝着正确方向的建议、方法和指示都将非常棒!此外,用于在客户端绘制位图的代码也在下面。
评论 Dmitriy 的回答/评论:
#region RootWorkItem
private ScreenClient m_RootWorkItem;
/// <summary>
/// Gets the RootWorkItem
/// </summary>
public ScreenClient RootWorkItem {
get {
if (m_RootWorkItem == null) {
m_RootWorkItem = new ScreenClient();
m_RootWorkItem.FrameRead += new EventHandler<FrameEventArgs>(RootWorkItem_FrameRead);
}
return m_RootWorkItem;
}
}
#endregion
private void RootWorkItem_FrameRead(Object sender, FrameEventArgs e) {
if (e.Frame != null) {
if (uxSurface.Image != null) {
Bitmap frame = (Bitmap)uxSurface.Image;
Graphics g = Graphics.FromImage(frame);
g.DrawImage(e.Frame, 0, 0); // Draw only updated pixels
uxSurface.Image = frame;
}
else {
uxSurface.Image = e.Frame; // Draw initial, full image
}
}
else {
uxSurface.Image = null;
}
}
【问题讨论】:
-
请注意,如果您尝试执行远程桌面之类的操作,则不是通过比较位图来确定要发送给客户端的内容。它在将绘图命令写入屏幕之前拦截它们。
-
看看你是否在opensubscriber.com/message/dotnet-cx@discuss.develop.com/…的线程中找到任何有用的提示。
-
@David:我想起了一些远程桌面软件用来处理与您描述的问题类似的计算机的策略:以较低的像素深度发送显示信息。如果您使用 8 位图形(至少对于比较部分),您的比较运行速度会快 4 倍。
-
@David Anderson:dreamincode.net/code/snippet2859.htm 非常快,但它确实使用了哈希。它还允许您在不编写自己的不安全代码的情况下获取字节数组(耶)。
-
教程解释了在 GDI+ 中使用 unsafe:codeproject.com/KB/GDI-plus/csharpgraphicfilters11.aspx
标签: c# performance gdi+