2010-07-30 92 views
3

我想調整一個RGB 8位TIF並保持它在c#中的透明度。我試過下面的代碼。調整tiff並保持透明度和c#

using (Image thumbnail = new Bitmap(1500, 1500)) 
{ 
    using (Bitmap source = new Bitmap(@"c:\trans.tif")) 
    { 
    using (Graphics g = Graphics.FromImage(thumbnail)) 
    { 
     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
     g.Clear(Color.Transparent); 
     g.DrawImage(source, 0, 0, 1500, 1500); 

    } 
    } 
    thumbnail.Save(@"c:\testoutput.tif", ImageFormat.Tiff); 
    //using (MemoryStream ms = new MemoryStream()) 
    //{ 
    // //thumbnail.Save(ms, ImageFormat.Tiff); 
    // // 
    // //result = ms.ToArray(); 
    //} 
} 

該圖像是使用photoshop創建的,我無法弄清楚爲什麼它沒有在調整大小的tiff上保持透明度。

任何想法?

+0

你爲什麼要專門調用'g.Clear(Color.Transparent);'? – 2010-07-30 14:19:07

+0

通過格式化代碼示例以獲得更好的可讀性,您已經足夠長時間向人們展示一些尊重。 – Oded 2010-07-30 14:20:14

回答

1

我實際上發現,由於透明度存儲在tiff格式與photoshop它wa通過自動化photoshop創建png,然後摳出png就更好了。

4

您需要設置像素格式,當你新達Image thumbnail至少32bpp的ARGB:

new Bitmap(1500, 1500, PixelFormat.Format32bppPArgb) 

編輯:

更妙的是,從source抓住它,那麼您一定完全相同的一個,只是意味着逆轉你的using s

相關問題