In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you what are several methods of fast processing of .NET pictures. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
First of all, we will introduce a class System.Drawing.Imaging.BitmapData. It is useless to instantiate this class directly. We need to lock a Bitmap into memory to get an instance of BitmapData. The fast processing methods for .net images are as follows:
Use Bitmap.LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format) or another of its overloaded Bitmap.LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format, BitmapData bitmapData) to lock the image data into memory to get an instance of BitmapData associated with the specified picture.
There is an important attribute in BitmapData, Scan0, which is a pointer to * * locations in the memory where the image data is located. Using memory tracking, fill in the address with the value of Scan0, and you can see the allocation of memory (Format32bppArgb color depth):
The corresponding relationship between these values and picture pixels is as follows:
Now we can use the method of System.Runtime.InteropServices.Marshal.WriteByte (IntPtr ptr, byteval) to change the pixel value of the specified location. After modification, just call Bitmap.UnlockBits (BitmapData bitmapdata) again to unlock the memory, for example:
Private void LockUnlockBitsExample (PaintEventArgs e) {Bitmap bmp = new Bitmap ("c:\\ fakePhoto.jpg"); Rectangle rect = new Rectangle (0,0, bmp.Width, bmp.Height); System.Drawing.Imaging.BitmapData bmpbmpData = bmp.LockBits (rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); IntPtr ptr = bmpData.Scan0; int bytes = bmp.Width * bmp.Height * 3; byte [] rgbValues = new byte [bytes]; for (int counter = 0; counter)
< rgbValues.Length; counter += 3) { Marshal.WriteByte(ptr, counter, 255); } bmp.UnlockBits(bmpData); e.Graphics.DrawImage(bmp, 0, 0); } 此.NET图片快速处理示例将图片上所有像素的Red向量设置为255。运行此实例可以看到图片变色了。 每次调用System.Runtime.InteropServices.Marshal.WriteByte(IntPtr ptr, byteval)的方法并不方便,因此我们构造一个ColorBgra类用来储存这4个颜色向量,它的主要代码是这样的(参考自Paint.Net提供的源码): [StructLayout(LayoutKind.Explicit)] public struct ColorBgra { [FieldOffset(0)] public byte B; [FieldOffset(1)] public byte G; [FieldOffset(2)] public byte R; [FieldOffset(3)] public byte A; /// /// Lets you change B, G, R, and A at the same time. /// [FieldOffset(0)] public uint Bgra; public override string ToString() { return "B: " + B + ", G: " + G + ", R: " + R + ", A: " + A; } } 使用这个类在声明为unsafe的上下文中就可以通过计算偏移量的办法寻址找到指定位置像素的地址(指针),例如在Format32bppArgb颜色深度的图片中可以这样计算: public unsafe ColorBgra* GetPointAddress(int x, int y) { return y * 4 + x; } 将计算返回的指针赋给ColorBgra*。之后使用如下方法: color->B = I; color-> G = I; color-> R = I; color-> A = I
Write the value directly into memory to realize the fast processing of .NET pictures.
The above are several ways to quickly deal with .NET pictures, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.