我正在尝试从.aspx页面返回透明GIF,以便在网页中显示.我试图让图像具有透明度,但我只是让黑色成为图像应该透明的地方.
有谁知道我做错了什么?
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
Chris Pietsc.. 7
是的,正如杰罗姆所说,无论如何都不能使用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
Jerome Laban.. 5
不幸的是,没有简单的方法可以使用Bitmap对象创建透明的Gif.(参见此知识库文章)
您也可以使用支持您正在使用的代码的透明度的PNG格式.
是的,正如杰罗姆所说,无论如何都不能使用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
不幸的是,没有简单的方法可以使用Bitmap对象创建透明的Gif.(参见此知识库文章)
您也可以使用支持您正在使用的代码的透明度的PNG格式.