0
私は2枚の画像を重ねて重ねて、最初のものを透明にします。私はこのコードを使用していますが、透明性はまったく機能していません。 2番目の画像は最初の画像を完全に覆っています。私はそれが1よりも小さいと不透明度値を入れて固定し、フレームワーク4.5、Visual Studioの2012オーバーレイ画像と別の透明画像
static void Main(string[] args)
{
Image imageBackground = Image.FromFile(@"e:\picstest\profile.png");
Image imageOverlay = SetImageOpacity(Image.FromFile(@"e:\picstest\flag.png"), 50);
Image img = new Bitmap(imageBackground.Width, imageBackground.Height);
using (Graphics gr = Graphics.FromImage(img))
{
gr.DrawImage(imageBackground, new Point(0, 0));
gr.DrawImage(imageOverlay, new Point(0, 0));
}
string outputFileName = @"e:\picstest\output.png";
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
{
img.Save(memory, ImageFormat.Jpeg);
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}
}
}
public static Image SetImageOpacity(Image img, float opacity)
{
Bitmap bmp = new Bitmap(img.Width, img.Height); // Determining Width and Height of Source Image
Graphics graphics = Graphics.FromImage(bmp);
ColorMatrix colormatrix = new ColorMatrix();
colormatrix.Matrix33 = opacity;
ImageAttributes imgAttribute = new ImageAttributes();
imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
graphics.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttribute);
graphics.Dispose(); // Releasing all resource used by graphics
return bmp;
}
}
}
ご使用の言語、オペレーティングシステム、使用しているライブラリ/ツールについて言及することをお勧めします。 –
なぜJPEGが透明をサポートしていない場合に 'ImageFormat.Jpeg'を使用していますか? –
私はpngに変更しましたが、同じ結果が得られませんでした。 –