所以,我用C#做了一些简单的image processing程序。 例如,我想更改HSV颜色模型中的图像颜色,将每个像素从RGB转换回来。
我的程序通过用户select加载一些图片,并使用其graphics上下文将其显示在窗体的一个面板中。 然后,用户可以通过移动滚动条,单击button,select一些图像区域等,使用此图片做一些事情。当他这样做,我需要实时改变所有的图片逐像素。 所以,我写了这样的东西:
for (int x = 0; x < imageWidth; x++) for (int y = 0; y < imageHeight; y++) Color c = g.GetPixel(x,y); c = some_process_color_function_depending_on_user_controls(c); g.SetPixel(x,y)
即使我在内存中使用graphics(而不是在屏幕上),函数GetPixel和SetPixel的工作速度非常慢(因为我的程序运行速度很慢,我对其进行了描述,并解释说这两个函数最多会减慢我的程序的速度)。 所以,当用户移动滑块或检查checkBox时,我无法在几秒钟内处理大图片。
请帮忙! 我能做些什么来使我的计划变得快速? 我同意使用其他第三方库进行graphics或改变编程语言!
Onenote开发
如何做一个“尊重”以前的控制台内容的C#Windows控制台应用程序?
C#WPF获取错误 – system.invalidOperationException:
MSI文件在安装后被复制的位置?
是否可以使用.Net框架以编程方式logging对Windows共享(SMB共享)的访问?
在本地主机上托pipe的REST API,无需pipe理权限
在.NET中检测远程DHCP服务器
In-Proc SxS在托pipe代码中打开shell扩展?
Windows应用程序或本地Web应用程序
是的,Get / SetPixel函数非常慢。 使用Bitmap.LockBits() / UnlockBits()代替。 它返回原始位数据供您操作。
从msdn参考:
private void LockUnlockBitsExample(PaintEventArgs e) { // Create a new bitmap. Bitmap bmp = new Bitmap("c:\fakePhoto.jpg"); // Lock the bitmap's bits. Rectangle rect = new Rectangle(0,bmp.Width,bmp.Height); System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect,System.Drawing.Imaging.ImageLockMode.ReadWrite,bmp.PixelFormat); // Get the address of the first line. IntPtr ptr = bmpData.Scan0; // Declare an array to hold the bytes of the bitmap. // This code is specific to a bitmap with 24 bits per pixels. int bytes = bmp.Width * bmp.Height * 3; byte[] rgbValues = new byte[bytes]; // copy the RGB values into the array. System.Runtime.InteropServices.Marshal.copy(ptr,rgbValues,bytes); // Set every red value to 255. for (int counter = 2; counter < rgbValues.Length; counter+=3) rgbValues[counter] = 255; // copy the RGB values back to the bitmap System.Runtime.InteropServices.Marshal.copy(rgbValues,ptr,bytes); // Unlock the bits. bmp.UnlockBits(bmpData); // Draw the modified image. e.Graphics.DrawImage(bmp,150); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。