C# lockbit 问题 本帖最后由 u012160576 于 2013-09-24 22:04:24 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 代码是这样: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循环中的判断没有=号 你这代码看的我伤心。 // <= 改成< 你没看到吗 32bppArgbfor (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);}24bppRgbfor (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位+3for (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; 在C#中查找和替换字符、字符串 急呀急呀求FTP上传高手帮助 如何从HTTP请求流中获取数据并保存为文件(可能是多个文件),在先等--急! 求助 C#中如何使程序存储上一次选择的打印机(分数:100+) 两个DATATABLE判断问题 一道高难度动态规划题(高手请进) AdoHelper能否改写成单例模式? C#如何实现进程同步? 请问.NET 的 . 怎么念? 念点吗? 关于反射的问题,没有找到合适的答案 求大仙DevExpress RepositoryItemCheckEdi t控件问题 为什么找不到?高手帮忙啊
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.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循环中的判断没有=号 你这代码看的我伤心。
{
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);
}24bppRgbfor (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);
}
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;