System.Drawingを使用して透明画像をどのように描画しますか?
-
06-07-2019 - |
質問
.aspxページから透明なGIFを返して、Webページ内に表示しようとしています。私は画像に透明性を持たせようとしていますが、画像を透明にする必要がある場所を黒にするだけです。
誰かが私が間違っていることを知っていますか?
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
解決
残念ながら、ビットマップオブジェクトを使用して透明なGifを作成する簡単な方法はありません。 (このKB記事を参照)
代わりに、使用しているコードで透明度をサポートするPNG形式を使用できます。
他のヒント
はい、ジェロームが述べたように、ビットマップオブジェクトを使用して透明な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
これは可能ですが、簡単ではありません
プロジェクトで安全でないコードを使用できる場合、ポインターを使用してカラーテーブルをリッピングし、透明度を機能させる方法がいくつかあります。
Bob Powellによるサンプルフォームアプリは、 http://www.bobpowell.net/giftransparencyから入手できます。 .htm 。数年前に月に約1,000万回ヒットするWebハンドラーでこのメソッドのバリエーションを使用しましたが、うまくいくようです。
限られたカラーパレットのみを使用している場合、カラーテーブルの処理を必要な色だけに減らすことができます(私がどうやってそれを行ったかを正確に思い出せません...)。
とはいえ、pngはメトリックスクラップロードの方が簡単です。
ここに、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&#233; goire Lafortune