2014-02-13 50 views
0

我正在使用以下代碼使用Windows GDI Library調整圖像大小。我正在嘗試生成透明圖像。該函數從from和被調用來生成圖像。如果我保存圖像不調整我越來越透明GDI透明背景C#

static Image FixedSize(Image imgPhoto, int Width, int Height) 
    { 
     int sourceWidth = imgPhoto.Width; 
     int sourceHeight = imgPhoto.Height; 
     int sourceX = 0; 
     int sourceY = 0; 
     int destX = 0; 
     int destY = 0; 

     float nPercent = 0; 
     float nPercentW = 0; 
     float nPercentH = 0; 

     nPercentW = ((float)Width/(float)sourceWidth); 
     nPercentH = ((float)Height/(float)sourceHeight); 
     if(nPercentH < nPercentW) 
     { 
      nPercent = nPercentH; 
      destX = System.Convert.ToInt16((Width - 
          (sourceWidth * nPercent))/2); 
     } 
     else 
     { 
      nPercent = nPercentW; 
      destY = System.Convert.ToInt16((Height - 
          (sourceHeight * nPercent))/2); 
     } 

     int destWidth = (int)(sourceWidth * nPercent); 
     int destHeight = (int)(sourceHeight * nPercent); 

     Bitmap bmPhoto = new Bitmap(Width, Height, 
          PixelFormat.Format24bppRgb); 
     bmPhoto.SetResolution(imgPhoto.HorizontalResolution, 
         imgPhoto.VerticalResolution); 

     Graphics grPhoto = Graphics.FromImage(bmPhoto); 
     grPhoto.Clear(Color.Transparent); 
     grPhoto.InterpolationMode = 
       InterpolationMode.HighQualityBicubic; 

     grPhoto.DrawImage(imgPhoto, 
      new Rectangle(destX,destY,destWidth,destHeight), 
      new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight), 
      GraphicsUnit.Pixel); 

     grPhoto.Dispose(); 
fileName= txtImgName.text(); 
     bitmap.Save(Server.MapPath("~/images32/") + fileName, ImageFormat.Png); 
    } 

我正在調整圖像,但問題是,它有黑色背景。 請幫我在生成透明圖像

由於提前

回答

0

我想這是因爲您使用RGB顏色(Format24bppRgb)作爲源位圖。

使用一個包含Alpha通道的文件,如Format32bppArgb