2017-01-14 6 views
0

画像のサイズを変更するこのメソッドを作成しましたが、透明なPNG画像の返された画像は黒い背景になります。 解決策は何ですか?Graphics.DrawImageを使用して、透明なPNG画像の背景を黒に変更します。

私はBitmap.MakeTransparent()を試しましたが、Graphics.Clear()も問題を解決できませんでした。 関連するすべての質問を確認しましたが、私の質問に対する有益な回答が見つかりませんでした。

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つのチャンネルに制限しています。このため、作成しているビットマップはアルファ(透明度)をサポートしておらず、デフォルトでは黒の黒い画像になります。透明部分を含む画像を描画する場合、その画像のアルファは、その形式に応じて、事前に乗算されるか破棄されます。

あなたは、透明性とあなたの新しいイメージを保存したい場合は、あなたがPixelFormat.Format32bppArgbでそれを宣言する必要があります:あなたのソリューションのための

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

おかげで、それは素晴らしい作品。画像はブラウザのキャッシュから読み込まれていましたが、あなたの解決策は機能していないと思いました。ありがとう。 –

関連する問題