Tag: lockbits

C#如何将获取的GetPixel / SetPixel颜色处理转换为Lockbits?

编辑:我非常感谢回复。 我在这里需要的不仅仅是示例代码,用于我在嵌套循环中使用几行代码所做的事情,因为这在GetPixel / SetPixel中是正常的,但也是我无法使用Lockbits工作的。 谢谢 我正在尝试将我的图像处理filter从GetPixel / SetPixel转换为Lockbits,以缩短处理时间。 我在Stack Overflow,MSDN和其他网站上也看过Lockbits教程,但是我做错了。 我从一个非常简单的filter开始,它简单地减少绿色以产生红色和紫色效果。 这是我的代码: private void redsAndPurplesToolStripMenuItem_Click(object sender, EventArgs e) { // Get bitmap from picturebox Bitmap bmpMain = (Bitmap)pictureBoxMain.Image.Clone(); // search through each pixel via x, y coordinates, examine and make changes. Dont let values exceed 255 or fall under 0. for (int y = 0; […]

.Net使用Lockbits从位图获取RGB值

我使用下面的代码从图像中提取RGB值,有时这是有效的,但是在某些文件上(似乎Stride不能被位图的宽度整除)它返回混合值: Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height) Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(rect, Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format24bppRgb) Dim ptr As IntPtr = bmpData.Scan0 Dim cols As New List(Of Color) Dim bytes As Integer = Math.Abs(bmpData.Stride) * bmp.Height Dim rgbValues(bytes – 1) As Byte System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes) ‘ Retrieve RGB values For i = modByte […]

直接读取和写入Unlocked Bitmap非托管内存(Scan0)

从解锁的Bitmap非托管内存直接写入和读取是否可以? 在我解锁Bitmap后,我可以继续使用BitmapData吗? 我做了一个测试应用程序,我可以在鼠标位置读取PictureBox的Bitmap像素,而另一个线程正在将像素写入同一个Bitmap。 编辑1:正如Boing在他的回答中指出的那样:“Scan0没有指向Bitmap对象的实际像素数据;而是指向一个临时缓冲区,它表示Bitmap对象中像素数据的一部分。” 来自MSDN 。 但是一旦我获得Scan0,我就能够读取/写入位图而无需Lockbits或UnlockBits! 我在一个post里做了很多次。 相对于MSDN,它不应该发生,因为Scan0指向位图数据的COPY! 好吧,在C#中,所有测试都显示它不是副本。 在C ++中,我不知道它是否正常工作。 编辑2:使用旋转方法有时会使操作系统释放位图像素数据副本。 结论, it is not safe to read/write an unlocked Bitmap Scan0 。 谢谢Boing的回答和评论! 下面是我如何获取BitmapData并读取和写入像素值。 /// /// Locks and unlocks the Bitmap to get the BitmapData. /// /// Bitmap /// BitmapData public static BitmapData GetBitmapData(Bitmap bmp) { BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, […]

如何使用LockBits将黄色位图中的非黑色像素着色?

使用GetPixel和SetPixel很容易但很慢,所以我正在尝试使用LockBits。 我有很久以前做过的这种方法来比较两个图像: public static Bitmap FastComparison(Bitmap bmp1,Bitmap bmp2) { tolerancenumeric = 15; int tolerance = tolerancenumeric * tolerancenumeric + tolerancenumeric * tolerancenumeric + tolerancenumeric * tolerancenumeric; //dr * dr + dg * dg + db * db; bmp3 = new Bitmap(512,512); PixelFormat pxf = PixelFormat.Format24bppRgb; Rectangle rect = new Rectangle(0, 0, bmp1.Width, bmp1.Height); BitmapData bmpData1 […]