11

Web ページ内に表示するために、.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
4

4 に答える 4

7

はい、ジェロームが述べたように、Bitmap オブジェクトを使用して透明な GIF を作成する方法はありません。くだらない!

とにかく、PNG を生成するようにコードを変更したところ、すべて期待どおりに動作しました。

PNG を OutputStream に直接書き込むことができないため、私が行う必要があった小さな回避策が 1 つあります。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
于 2008-10-09T22:41:07.297 に答える
5

残念ながら、Bitmap オブジェクトを使用して透明な Gif を作成する簡単な方法はありません。(この KB 記事を参照してください)

代わりに、使用しているコードで透過性をサポートする PNG 形式を使用することもできます。

于 2008-10-09T22:29:13.063 に答える
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 at https://web.archive.org/web/20141227173018/http://bobpowell.net/giftransparency.aspx. I used a variation on this method in a web handler, and it seemed to work fine.

If you are only using a limited colour palette, 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 substantially easier.

于 2008-10-09T23:11:26.520 に答える
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);

幸運を !

グレゴワール・ラフォーチュン

于 2010-01-26T16:26:11.407 に答える