1.GetPixel+SetPixel
public bool match(Color pc) { Color sc = Color.FromArgb(130, 9, 198); if (System.Math.Abs(pc.R - sc.R) < 10 && System.Math.Abs(pc.G - sc.G) < 10 && System.Math.Abs(pc.B - sc.B) < 10) { return true; } return false; } public void cr(string filename) { Bitmap bm = new Bitmap(filename);//BMP文件路徑 Color bg = Color.FromArgb(255, 255, 255); bool flag = false; int max = 5; int count = max; System.DateTime dt = System.DateTime.Now; for (int i = 0; i < bm.Width; ) { for (int j = 0; j < bm.Height; ) { Color c = bm.GetPixel(i, j); if (match(c)) { if (!flag) { for (int k = j - 1; k > j - count && k >= 0; k--) { Color tc = bm.GetPixel(i, k); if (match(tc)) { bm.SetPixel(i, k, bg); } else break; } flag = true; count = 1; } bm.SetPixel(i, j, bg); j = j + 1; } else { flag = false; count = count < max ? count + 1 : max; j = j + count; } } i = i + 1; } System.IO.MemoryStream ms = new System.IO.MemoryStream(); bm.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); bm.Dispose(); Response.ClearContent(); Response.ContentType = "image/JPEG"; Response.BinaryWrite(ms.ToArray()); ms.Close(); ms.Dispose(); }
2.DrawImage + ColorMap
public void cr(string filename) { Bitmap bm = new Bitmap(filename); // Create a color map. ColorMap[] myColorMap = new ColorMap[1]; myColorMap[0] = new ColorMap(); myColorMap[0].OldColor = Color.FromArgb(130, 9, 198); myColorMap[0].NewColor = Color.FromArgb(255, 255, 255); // Create an ImageAttributes object, and then pass the // myColorMap object to the SetRemapTable method. ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetRemapTable(myColorMap,ColorAdjustType.Bitmap); int w = bm.Width; int h = bm.Height; Rectangle rect = new Rectangle(0, 0, w, h); Graphics GDI = Graphics.FromImage(bm); GDI.DrawImage(bm, rect, 0, 0, w, h, GraphicsUnit.Pixel, imageAttr); GDI.Dispose(); System.IO.MemoryStream ms = new System.IO.MemoryStream(); bm.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); bm.Dispose(); Response.ClearContent(); Response.ContentType = "image/JPEG"; Response.BinaryWrite(ms.ToArray()); ms.Close(); ms.Dispose(); }