旧ブログからのコード移植、その2。
SetAlpha32 は PixelFormat.Format32bppArgb の元画像のアルファ値を
均一に設定した値に減少させる。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using ImageUtils; namespace LuminanceToAlpha { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static bool LuminanceToAlpha(Bitmap bmp, bool bStretch, Color baseColor, out Bitmap result32) { result32 = null; if (bmp.PixelFormat != PixelFormat.Format24bppRgb) return false; int w = bmp.Width; int h = bmp.Height; Bitmap bw = bmp.Clone() as Bitmap; ImgUtils.GrayScale(ref bw); if (bStretch) ImgUtils.HistoStretch(ref bw); result32 = new Bitmap(w, h, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(result32); g.Clear(baseColor); g.Dispose(); BmpProc8 src = new BmpProc8(bw); BmpProc32 dst = new BmpProc32(result32); for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) { dst[x, y, eRGB.a] = (byte)(255 - src[x, y]); } ImgUtils.CallDispose(dst, src, bw); return true; } public static bool SetAlpha32(ref Bitmap bmp, int percent) { if (bmp.PixelFormat != PixelFormat.Format32bppArgb) return false; int w = bmp.Width; int h = bmp.Height; double p = percent / 100d; BmpProc32 dst = new BmpProc32(bmp); for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) { if (dst[x, y, eRGB.a] == 0) continue; dst[x, y, eRGB.a] = ImgUtils.AdjustByte(dst[x, y, eRGB.a] * p); } dst.Dispose(); return true; } private void button1_Click(object sender, EventArgs e) { Bitmap bmp = new Bitmap(@"c:\Home\ImgWork\SASample2.png"); int w = bmp.Width; int h = bmp.Height; Bitmap tmp1, tmp2; Color baseColor = Color.FromArgb(10, 00, 70); LuminanceToAlpha(bmp, true, baseColor, out tmp1); Graphics g = this.CreateGraphics(); for (int i = 0; i < 5; i++) { tmp2 = tmp1.Clone() as Bitmap; SetAlpha32(ref tmp2, 15 + 20 * i); g.DrawImage(tmp2, 20 + 150 * i, 50 + 30 * i, w, h); tmp2.Dispose(); } g.Dispose(); tmp1.Dispose(); bmp.Dispose(); } } }
※コメント投稿者のブログIDはブログ作成者のみに通知されます