C# lockbit 问题
--------------------编程问答-------------------- 代码是这样:Bitmap source = new Bitmap(pictureBox1.Image);
BitmapData sourceData = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
IntPtr source_scan = sourceData.Scan0;
unsafe
{
byte* p = (byte*)source_scan.ToPointer();
for (int width = 0; width <= source.Width; ++width)
{
for (int height = 0; height <= sourceData.Height; ++height)
{
int r, g, b;
r = p[2];
g = p[1];
b = p[0];
p[0] = p[1] = p[2] = (byte)(.33 * r + .33 * g + .33 * b);
p += 3;
}
p += sourceData.Stride - (sourceData.Width * 3);
}
}
source.UnlockBits(sourceData);
pictureBox1.Image=source; --------------------编程问答-------------------- PixelFormat.Format32bppArgb改为PixelFormat.Format24bpprgb --------------------编程问答--------------------
但我写成PixelFormat.Format24bpprgb的时候
R = P [2];这边就有问题 --------------------编程问答--------------------
unsafe
{
byte* p = (byte*)source_scan.ToPointer();
for (int height = 0; height < sourceData.Height; ++height) // <= 改成<
{
for (int width = 0; width < sourceData.Width; ++width) // <= 改成<
{
int r, g, b;
r = p[2];
g = p[1];
b = p[0];
p[0] = p[1] = p[2] = (byte)(.33 * r + .33 * g + .33 * b);
p += 3;
}
p += sourceData.Stride - (sourceData.Width * 3);
}
}
两个for循环调换位置, for循环中的判断没有=号
你这代码看的我伤心。
--------------------编程问答--------------------
不好意思 我是初学者 我把循环调换位置的问题一样出现 說"尝试读取或写入受保护的记忆体。这通常表示其他记忆体已损毁" --------------------编程问答-------------------- // <= 改成< 你没看到吗 --------------------编程问答--------------------
我把他改好了 现在沒有出现错误,但是我这段码颜色没有改变 是什么原因吗? --------------------编程问答-------------------- 32bppArgb
for (int height = 0; height < sourceData.Height; height++)
{
for (int width = 0; width < source.Width; width++)
{
int r, g, b;
r = p[2];
g = p[1];
b = p[0];
p[0] = p[1] = p[2] = (byte)(.33 * r + .33 * g + .33 * b);
p += 4;
}
p += sourceData.Stride - (sourceData.Width * 4);
}
24bppRgb
--------------------编程问答-------------------- 这样也可以
for (int height = 0; height < sourceData.Height; height++)
{
for (int width = 0; width < source.Width; width++)
{
int r, g, b;
r = p[2];
g = p[1];
b = p[0];
p[0] = p[1] = p[2] = (byte)(.33 * r + .33 * g + .33 * b);
p += 3;
}
p += sourceData.Stride - (sourceData.Width * 3);
}
--------------------编程问答-------------------- 撸主你真的明白这些代码每一行的意思么?
Bitmap source = new Bitmap(pictureBox1.Image);
BitmapData sourceData = source.LockBits(new Rectangle(0, 0, source.Width,
source.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
IntPtr ptr = sourceData.Scan0;
byte[] buffer = new byte[sourceData.Stride * sourceData.Height];
System.Runtime.InteropServices.Marshal.Copy(ptr, buffer, 0, buffer.Length);
//32位+4 24位+3
for (int i = 0; i < buffer.Length; i += 4)
{
byte b = buffer[i+0];
byte g = buffer[i+1];
byte r = buffer[i+2];
buffer[i+0] = buffer[i+1] = buffer[i+2] = (byte)(0.33 * b + 0.33 * g + 0.33 * r);
}
System.Runtime.InteropServices.Marshal.Copy(buffer, 0, ptr, buffer.Length);
source.UnlockBits(sourceData);
pictureBox1.Image = source;
补充:.NET技术 , C#