string sp = Server.MapPath("/");//"/PDF/tz/tempword/" if (!System.IO.Directory.Exists(sp)) { System.IO.Directory.CreateDirectory(sp); } //Request.Files[0].SaveAs(path); ImageAttributes vAttr = new ImageAttributes(); //vAttr是关键 //图像中与0,0那点相同的颜色要变成透明 Bitmap bitmap = new Bitmap(Request.Files[0].InputStream); Bitmap bitmappng = new Bitmap(165, 165);//新生成的图片宽与高 vAttr.SetColorKey(bitmap.GetPixel(0, 0), bitmap.GetPixel(0, 0)); Graphics g = Graphics.FromImage(bitmappng); g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(bitmap, new Rectangle(10, 10, 165, 165), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, vAttr); g.Save(); g.Dispose(); bitmappng.Save(sp + "png.png", ImageFormat.Png);
ImageAttributes vAttr = new ImageAttributes(); //vAttr是关键 //图像中与0,0那点相同的颜色要变成透明 Bitmap bitmap = new Bitmap(Request.Files[0].InputStream); Bitmap bitmappng = new Bitmap(165, 165); bitmap.SetPixel(0, 0, Color.FromArgb(0, 0, 0));//设置为统一颜色 /*将图片的背景只留下红色*/ for (int i = 0; i < bitmap.Width; i++) { for (int j = 0; j < bitmap.Height; j++) { System.Drawing.Color c = bitmap.GetPixel(i, j); /*找到红色的RGB范围,然后再进行替换*/ if (c.R > 128 && (c.R > c.G + 20 && c.R > c.B + 20)) /*非红色要替换成透明 每种颜色要存在一定的差距*/ { //bitmappng.SetPixel(i, j, System.Drawing.Color.Transparent); //bitmap.SetPixel(i, j, Color.FromArgb(0,c.R, c.G, c.B));//设置为统一颜色 } else { bitmap.SetPixel(i, j, Color.FromArgb(0, 0, 0, 0));//设置为统一颜色 } } } vAttr.SetColorKey(bitmap.GetPixel(0, 0), bitmap.GetPixel(0, 0)); Graphics g = Graphics.FromImage(bitmappng); g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(bitmap, new Rectangle(0, 0, 165, 165), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, vAttr); g.Save(); g.Dispose(); bitmappng.Save(path, ImageFormat.Png);