Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
半透明のイメージを生成する。
using System.Drawing;
using System.Drawing.Imaging;
/// <summary>
/// 半透明のイメージを生成する。
/// </summary>
/// <param name="sourceImage">元イメージ。</param>
/// <returns>半透明化したイメージ。</returns>
public static Image CreateAlphaImage(Image sourceImage)
{
int imageWidth = sourceImage.Width;
int imageHeight = sourceImage.Height;
// 新しいビットマップを用意
Bitmap alphaImage = new Bitmap(imageWidth, imageHeight);
using (Graphics g = Graphics.FromImage(alphaImage))
{
// ColorMatrixオブジェクトの作成
ColorMatrix cm = new ColorMatrix();
// ColorMatrixの行列の値を変更して、アルファ値が0.5に変更されるようにする
cm.Matrix00 = 1;
cm.Matrix11 = 1;
cm.Matrix22 = 1;
cm.Matrix33 = 0.5F;
cm.Matrix44 = 1;
// ImageAttributesオブジェクトの作成
ImageAttributes ia = new ImageAttributes();
// ColorMatrixを設定
ia.SetColorMatrix(cm);
// ImageAttributesを使用して画像を描画
g.DrawImage(sourceImage,
new Rectangle(0, 0, imageWidth, imageHeight),
0,
0,
imageWidth,
imageHeight,
GraphicsUnit.Pixel,
ia);
}
return alphaImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.