I'm trying to return a transparent GIF from an .aspx page for display within a web page. I am trying to get the image to have transparency, but I just keep getting Black being where the image should be Transparent.
我正在尝试从.aspx页面返回透明GIF,以便在网页中显示。我试图让图像具有透明度,但我只是让黑色成为图像应该透明的地方。
Does anyone know what I'm doing wrong?
有谁知道我做错了什么?
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
4 个解决方案
#1
5
Unfortunately, there is no easy way to create a transparent Gif using a Bitmap object. (See this KB article)
不幸的是,没有简单的方法可以使用Bitmap对象创建透明的Gif。 (见这篇知识库文章)
You can alternatively use the PNG format that supports transparency with the code you are using.
您也可以使用支持所用代码透明度的PNG格式。
#2
6
Yes, as Jerome stated, there isn't anyway to create transparent GIF's using a Bitmap object. Crap!
是的,正如Jerome所说,无论如何都不能使用Bitmap对象创建透明的GIF。扯淡!
Well, anyway, I changed my code to generate a PNG and all works as expected.
好吧,无论如何,我改变了我的代码以生成一个PNG,并且所有工作都按预期工作。
There is one small work around I did need to do since you cannot write PNG's directly to the OutputStream. I needed to write the PNG to a MemoryStream, and then write that out to the OutputStream.
我确实需要做一件小工作,因为你不能直接将PNG写入OutputStream。我需要将PNG写入MemoryStream,然后将其写入OutputStream。
Here's the final code for my implementation:
这是我实现的最终代码:
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
#3
4
It is possible, but not easy
If you are able to use unsafe code in your project, there are a few methods to use pointers to rip through the colour table and make the transparency work.
这是可能的,但并不容易如果您能够在项目中使用不安全的代码,有一些方法可以使用指针来翻阅颜色表并使透明度工作。
A sample forms app by Bob Powell is available here http://www.bobpowell.net/giftransparency.htm. I used a variation on this method on a web handler a few years ago that was getting hit about 10 million times a month, and it seemed to work fine.
Bob Powell的样本表单应用程序可在此处获取http://www.bobpowell.net/giftransparency.htm。几年前我在网络处理程序上使用了这种方法的一种变体,每个月都会受到大约1000万次的攻击,而且似乎工作得很好。
If you are only using a limited colour pallete you can reduce the colour table processing to just the colours you need (can't remember exactly how I did that...).
如果你只使用有限颜色的托盘,你可以将颜色表处理减少到你需要的颜色(不记得我是怎么做的......)。
That being said, png is a metric crapload easier.
话虽这么说,png是一个更容易的度量标准。
#4
2
here is some code to have a gif (that already have transparency in it) transformed (supposed you want to resize it) in bitmap and then can be showed properly with it's transparency.
这里有一些代码在位图中有一个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);
Good luck !
祝你好运 !
Grégoire Lafortune
GrégoireLafortune
#1
5
Unfortunately, there is no easy way to create a transparent Gif using a Bitmap object. (See this KB article)
不幸的是,没有简单的方法可以使用Bitmap对象创建透明的Gif。 (见这篇知识库文章)
You can alternatively use the PNG format that supports transparency with the code you are using.
您也可以使用支持所用代码透明度的PNG格式。
#2
6
Yes, as Jerome stated, there isn't anyway to create transparent GIF's using a Bitmap object. Crap!
是的,正如Jerome所说,无论如何都不能使用Bitmap对象创建透明的GIF。扯淡!
Well, anyway, I changed my code to generate a PNG and all works as expected.
好吧,无论如何,我改变了我的代码以生成一个PNG,并且所有工作都按预期工作。
There is one small work around I did need to do since you cannot write PNG's directly to the OutputStream. I needed to write the PNG to a MemoryStream, and then write that out to the OutputStream.
我确实需要做一件小工作,因为你不能直接将PNG写入OutputStream。我需要将PNG写入MemoryStream,然后将其写入OutputStream。
Here's the final code for my implementation:
这是我实现的最终代码:
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
#3
4
It is possible, but not easy
If you are able to use unsafe code in your project, there are a few methods to use pointers to rip through the colour table and make the transparency work.
这是可能的,但并不容易如果您能够在项目中使用不安全的代码,有一些方法可以使用指针来翻阅颜色表并使透明度工作。
A sample forms app by Bob Powell is available here http://www.bobpowell.net/giftransparency.htm. I used a variation on this method on a web handler a few years ago that was getting hit about 10 million times a month, and it seemed to work fine.
Bob Powell的样本表单应用程序可在此处获取http://www.bobpowell.net/giftransparency.htm。几年前我在网络处理程序上使用了这种方法的一种变体,每个月都会受到大约1000万次的攻击,而且似乎工作得很好。
If you are only using a limited colour pallete you can reduce the colour table processing to just the colours you need (can't remember exactly how I did that...).
如果你只使用有限颜色的托盘,你可以将颜色表处理减少到你需要的颜色(不记得我是怎么做的......)。
That being said, png is a metric crapload easier.
话虽这么说,png是一个更容易的度量标准。
#4
2
here is some code to have a gif (that already have transparency in it) transformed (supposed you want to resize it) in bitmap and then can be showed properly with it's transparency.
这里有一些代码在位图中有一个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);
Good luck !
祝你好运 !
Grégoire Lafortune
GrégoireLafortune