2017-01-14 528 views
0

我已經編寫了此方法來調整圖像大小,但是透明PNG圖像的返回圖像具有黑色背景。 解決方案是什麼?使用Graphics.DrawImage,將透明PNG圖像的背景更改爲黑色

我試過Bitmap.MakeTransparent()也但無法解決我的問題。 我檢查了所有相關的問題,但找不到任何有用的答案給我的問題。

public HttpResponseMessage ImageResizer(string path, int w,int h) 
    { 

     var imagePath = HttpContext.Current.Server.MapPath(path); 
     Bitmap image; 
     try 
     { 
      image = (Bitmap)System.Drawing.Image.FromFile(imagePath); 
     } 
     catch 
     { 
      HttpResponseMessage hrm = new HttpResponseMessage(); 
      return hrm; 
     } 

     int originalWidth = image.Width; 
     int originalHeight = image.Height; 

     // New width and height based on aspect ratio 
     int newWidth = w; 
     int newHeight = h; 

     // Convert other formats (including CMYK) to RGB. 
     Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb); 
     // Draws the image in the specified size with quality mode set to HighQuality 
     using (Graphics graphics = Graphics.FromImage(newImage)) 
     { 
      graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      graphics.SmoothingMode = SmoothingMode.AntiAlias; 
      graphics.CompositingQuality = CompositingQuality.HighQuality; 
      graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; 

      using (var attribute = new ImageAttributes()) 
      { 
       attribute.SetWrapMode(WrapMode.TileFlipXY); 

       // draws the resized image to the bitmap 
       graphics.DrawImage(image, new Rectangle(0, 0, newImage.Width, newImage.Height), new Rectangle(0, 0, originalWidth, originalHeight), GraphicsUnit.Pixel); 
      } 
     } 
     // Get an ImageCodecInfo object that represents the PNG codec. 
     ImageCodecInfo imageCodecInfo = this.GetEncoderInfo(ImageFormat.Png); 

     // Create an Encoder object for the Quality parameter. 
     Encoder encoder = Encoder.Quality; 

     // Create an EncoderParameters object. 
     EncoderParameters encoderParameters = new EncoderParameters(1); 

     // Save the image as a PNG file with quality level. 
     EncoderParameter encoderParameter = new EncoderParameter(encoder, 10); 
     encoderParameters.Param[0] = encoderParameter; 
     var splitPath = imagePath.Split('.'); 
     string newPath = splitPath[0] + "ss5.png"; 
     newImage.Save(newPath, ImageFormat.Png); 

     MemoryStream memoryStream = new MemoryStream(); 
     newImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png); 
     HttpResponseMessage response = new HttpResponseMessage(); 
     response.Content = new ByteArrayContent(memoryStream.ToArray()); 
     response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/png"); 
     response.Content.Headers.ContentLength = memoryStream.Length; 
     return response; 
    } 

    private ImageCodecInfo GetEncoderInfo(ImageFormat format) 
    { 
     return ImageCodecInfo.GetImageDecoders().SingleOrDefault(c => c.FormatID == format.Guid); 
    } 

回答

0

你在你的Bitmap構造使用PixelFormat.Format24bppRgb。該格式將您的位圖限制爲3個通道:紅色,綠色和藍色。因此,您創建的位圖不支持alpha(a.k.a.透明度),並且默認爲純黑色圖像。當您繪製透明圖像時,該圖像的alpha值將被預倍或捨棄,具體取決於其格式。

如果你想保存透明度新圖片,你需要PixelFormat.Format32bppArgb聲明它:

Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppArgb); 
+0

感謝您的解決方案,它的偉大工程。圖像仍然從瀏覽器緩存加載,我認爲你的解決方案無法正常工作。謝謝。 –

相關問題