2010-08-13 158 views
2

我正在使用下面的代码来调整tif大小。 tif具有透明度设置的alpha通道。我正在尝试调整这张图片的尺寸,并尊重透明度,但此刻它会以黑色背景出现。有任何想法吗?Alpha通道透明度和调整图像文件大小

public static void ResizeImage(string OriginalImagePath, string NewImagePath, int Width, int Height) 
     { 
      Size NewSize = new Size(Width, Height); 

      using (Image OriginalImage = Image.FromFile(OriginalImagePath)) 
      { 
       //Graphics objects can not be created from bitmaps with an Indexed Pixel Format, use RGB instead. 
       PixelFormat Format = OriginalImage.PixelFormat; 
       if (Format.ToString().Contains("Indexed")) 
        Format = PixelFormat.Format24bppRgb; 

       using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height, OriginalImage.PixelFormat)) 
       { 
        using (Graphics Canvas = Graphics.FromImage(NewImage)) 
        { 
         Canvas.SmoothingMode = SmoothingMode.AntiAlias; 
         Canvas.InterpolationMode = InterpolationMode.HighQualityBicubic; 
         Canvas.PixelOffsetMode = PixelOffsetMode.HighQuality; 
         Canvas.DrawImage(OriginalImage, new Rectangle(new Point(0, 0), NewSize)); 
         NewImage.Save(NewImagePath, OriginalImage.RawFormat); 
        } 
       } 
      } 
     } 

    } 
+0

1)对于您遇到问题的图像,OriginalImage.PixelFormat的值是多少? 2)尝试保存到PNG。这仍然会给你黑色的像素? – 2010-08-16 20:56:07

回答

0

我实际上发现,由于透明度采用photoshop存储在tiff格式中,所以最好是通过自动化photoshop创建png,然后抠出png。

0

尝试这种情况:

if (Format.ToString().Contains("Indexed")) 
    Format = PixelFormat.Format32bppArgb; 

Format32bppArgb指定像素格式alpha通道。

而且我觉得你的意思是这样:

using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height, Format)) 

编辑:

事实上,尽量只强制在NewImage像素格式Format32bppArgb像这样:

using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height, 
    PixelFormat.Format32bppArgb)) 
+0

尝试了您所建议的更改,但仍以黑色背景显示。 – RubbleFord 2010-08-13 09:17:20

0
Canvas.Clear(Color.Transparent) 

之前blit。

+0

试过,没有运气。 – RubbleFord 2010-08-16 08:18:45

+0

并且您是否将Codesleuth建议的新位图的像素格式设置为Format32bppRgb? 24位颜色不会透明,不存在Alpha通道。 – Tergiver 2010-08-16 14:20:46