2008-10-09 76 views
10

我試圖從.aspx頁面返回一個透明的GIF在網頁中顯示。我試圖讓圖像具有透明度,但我只是不斷讓黑色成爲圖像應該透明的地方。如何使用System.Drawing繪製透明圖像?

有誰知道我在做什麼錯?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ 
    Handles Me.Load 
    '' Change the response headers to output a GIF image. 
    Response.Clear() 
    Response.ContentType = "image/gif" 

    Dim width = 110 
    Dim height = width 

    '' Create a new 32-bit bitmap image 
    Dim b = New Bitmap(width, height) 

    '' Create Grahpics object for drawing 
    Dim g = Graphics.FromImage(b) 

    Dim rect = New Rectangle(0, 0, width - 1, height - 1) 

    '' Fill in with Transparent 
    Dim tbrush = New System.Drawing.SolidBrush(Color.Transparent) 
    g.FillRectangle(tbrush, rect) 

    '' Draw Circle Border 
    Dim bPen = Pens.Red 
    g.DrawPie(bPen, rect, 0, 365) 

    '' Fill in Circle 
    Dim cbrush = New SolidBrush(Color.LightBlue) 
    g.FillPie(cbrush, rect, 0, 365) 


    '' Clean up 
    g.Flush() 
    g.Dispose() 

    '' Make Transparent 
    b.MakeTransparent() 

    b.Save(Response.OutputStream, Imaging.ImageFormat.Gif) 
    Response.Flush() 
    Response.End() 
End Sub 
+0

我刪除了我的帖子,所以它不會混亂這個了。 – mattlant 2008-10-09 23:34:23

回答

5

不幸的是,沒有簡單的方法使用Bitmap對象創建透明的Gif。 (請參閱this KB article

您也可以使用支持透明度的PNG格式與您正在使用的代碼。

6

是的,正如傑羅姆所說,沒有辦法用Bitmap對象創建透明的GIF。扯淡!

好吧,無論如何,我改變了我的代碼來生成一個PNG,並按預期工作。

由於無法將PNG直接寫入OutputStream,因此我需要做一點小小的工作。我需要將PNG寫入MemoryStream,然後將其寫入OutputStream。

下面是我實現的最終代碼:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ 
    Handles Me.Load 
    '' Change the response headers to output a JPEG image. 
    Response.Clear() 
    Response.ContentType = "image/png" 

    Dim width = 11 
    Dim height = width 

    '' Create a new 32-bit bitmap image 
    Dim b = New Bitmap(width, height) 

    '' Create Grahpics object for drawing 
    Dim g = Graphics.FromImage(b) 

    '' Fill the image with a color to be made Transparent after drawing is finished. 
    g.Clear(Color.Gray) 

    '' Get rectangle where the Circle will be drawn 
    Dim rect = New Rectangle(0, 0, width - 1, height - 1) 

    '' Draw Circle Border 
    Dim bPen = Pens.Black 
    g.DrawPie(bPen, rect, 0, 365) 

    '' Fill in Circle 
    Dim cbrush = New SolidBrush(Color.Red) 
    g.FillPie(cbrush, rect, 0, 365) 

    '' Clean up 
    g.Flush() 
    g.Dispose() 

    '' Make Transparent 
    b.MakeTransparent(Color.Gray) 

    '' Write PNG to Memory Stream then write to OutputStream 
    Dim ms = New MemoryStream() 
    b.Save(ms, Imaging.ImageFormat.Png) 
    ms.WriteTo(Response.OutputStream) 

    Response.Flush() 
    Response.End() 
End Sub 
4

這是可能但不容易
如果你能在你的項目中使用不安全的代碼,有幾個方法使用指針來翻轉顏色表並使透明度工作。

鮑勃鮑威爾的示例表格應用程序可在這裏獲得http://www.bobpowell.net/giftransparency.htm。幾年前,我在網絡處理程序上使用了這種方法的一個變體,每個月它的瀏覽次數大約是1000萬次,而且看起來效果不錯。

如果您只使用有限的顏色托盤,則可以將顏色表處理降低到所需的顏色(不記得我是如何做到的......)。

這就是說,png是一個更容易的度量crapload。

2

這裏有一些代碼在位圖中有一個gif(已經有透明度)轉換(假設你想調整大小),然後可以正確顯示它的透明度。

imagePath = System.Web.HttpContext.Current.Request.MapPath(libraryPath + reqImageFile); 
System.Drawing.Image image = null; 
Bitmap resizedImage = null; 

if (reqWidth == 0) { reqWidth = image.Width; } 
if (reqHeight == 0) { reqHeight = image.Height; } 
image = System.Drawing.Image.FromFile(imagePath); 
reqWidth = image.Width; 
reqHeight = image.Height; 

//here is the transparency 'special' treatment 
resizedImage = new Bitmap(reqWidth, reqHeight, PixelFormat.Format8bppIndexed); 
ColorPalette pal = resizedImage.Palette; 
for (int i = 0; i < pal.Entries.Length; i++) 
{ 
     Color col = pal.Entries[i]; 
     pal.Entries[i] = Color.FromArgb(0, col.R, col.G, col.B); 
} 
resizedImage.Palette = pal; 
BitmapData src = ((Bitmap)image).LockBits(new Rectangle(0, 0, reqWidth, reqHeight), ImageLockMode.ReadOnly, image.PixelFormat); 
BitmapData dst = resizedImage.LockBits(new Rectangle(0, 0, resizedImage.Width, resizedImage.Height), 
ImageLockMode.WriteOnly, resizedImage.PixelFormat); 
((Bitmap)image).UnlockBits(src); 
resizedImage.UnlockBits(dst); 

祝你好運!

GrégoireLafortune