C#2005 Imagelistの余白の除去について
お世話になります。
ListviewにImagelistを代入し、表示するものを作成しております。
枠のサイズは100×100で、実際のjpeg画像は約100×75です。
しかしながら、画像以外の箇所の色指定もしくは透過色を指定したいのですが、変更されません。
以下にサンプルコードと画面サンプルを登録させていただきます。
// 幅w、高さhのImageオブジェクトを作成
Image createThumbnail(Image image, int w, int h)
{
Bitmap canvas = new Bitmap(w, h);
Graphics g = Graphics.FromImage(canvas);
g.FillRectangle(new SolidBrush(Color.White), 0, 0, w, h);
float fw = (float)w / (float)image.Width;
float fh = (float)h / (float)image.Height;
float scale = Math.Min(fw, fh);
fw = image.Width * scale;
fh = image.Height * scale;
g.DrawImage(image, (w - fw) / 2, (h - fh) / 2, fw, fh);
g.Dispose();
return canvas;
}
private void button2_Click(object sender, EventArgs e)
{
string imageDir = @"C:\thumbnailTest\Image"; // 画像ディレクトリ
string[] jpgFiles =
System.IO.Directory.GetFiles(imageDir, "*.jpg");
int width = 100;
int height = 100;
imageList1.ImageSize = new Size(width, height);
listView1.LargeImageList = imageList1;
imageList1.TransparentColor = Color.Transparent;
for (int i = 0; i < jpgFiles.Length; i++)
{
Image original = Bitmap.FromFile(jpgFiles[i]);
Image thumbnail = createThumbnail(original, width, height);
imageList1.ColorDepth = ColorDepth.Depth24Bit;
imageList1.Images.Add(thumbnail);
listView1.Items.Add(jpgFiles[i], i);
original.Dispose();
thumbnail.Dispose();
}
}
お手数ですが、ご教授いただきたく宜しくお願い申し上げます。
お礼
回答ありがとうございます。 添付サイト参考にさせて頂きます。