8,735
社区成员
发帖
与我相关
我的任务
分享
#region 方法一:修改byte[]的颜色
//将图片转成byte数组存入内存中,在数组中修改鼠标点击的颜色, i:B i+1:G i+2:R i+3:Alpha 颜色一致的话改为 R:0xff,B:0xff,G:0xff Alpha:0
BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
byte[] maskImageRGBData = new byte[bmpData.Stride * bmpData.Height];
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, maskImageRGBData, 0, maskImageRGBData.Length);
//鼠标选中的当前颜色
byte[] curColorBytes = new byte[3];
//curColorBytes[0] = 0xff;
//curColorBytes[1] = 0xff;
//curColorBytes[2] = 0xff;
//curColorBytes[3] = 0;
curColorBytes[0] = currentColor.B;
curColorBytes[1] = currentColor.G;
curColorBytes[2] = currentColor.R;
for (int j = 0; j + 2 < maskImageRGBData.Length; j += 4)
{
if(curColorBytes[0]==maskImageRGBData[j]&&curColorBytes[1]==maskImageRGBData[j+1]&&curColorBytes[2]==maskImageRGBData[j+2])
{
maskImageRGBData[j] = 0xff;
maskImageRGBData[j+1] = 0xff;
maskImageRGBData[j+2] = 0xff;
maskImageRGBData[j+3] = 0;
}
}
System.Runtime.InteropServices.Marshal.Copy(maskImageRGBData, 0, bmpData.Scan0, maskImageRGBData.Length);
bmp.UnlockBits(bmpData);
#endregion
#region 方法二:直接修改该像素点的值 currentColor即为鼠标获取到的像素点
if (bmp != null)
{
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
cur = bmp.GetPixel(i, j);
if (cur.Equals(currentColor))
{
bmp.SetPixel(i, j, System.Drawing.Color.FromArgb(0x00, 0xff, 0xff, 0xff));//System.Drawing.Color.Transparent); //
bmp.MakeTransparent(cur);
}
}
}
}
#endregion
bmp.Save("d://111.png");
private void MakeTransparent_Example2(PaintEventArgs e)
{
// Create a Bitmap object from an image file.
Bitmap myBitmap = new Bitmap("Grapes.gif");
// Draw myBitmap to the screen.
e.Graphics.DrawImage(
myBitmap, 0, 0, myBitmap.Width, myBitmap.Height);
// Get the color of a background pixel.
Color backColor = myBitmap.GetPixel(1, 1);
// Make backColor transparent for myBitmap.
myBitmap.MakeTransparent(backColor);
// Draw the transparent bitmap to the screen.
e.Graphics.DrawImage(
myBitmap, myBitmap.Width, 0, myBitmap.Width, myBitmap.Height);
}