C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 91 を表示中)
■54189 / )  Re[4]: 画像データからバイト配列
□投稿者/ SUKIYA (4回)-(2010/10/08(Fri) 12:28:22)
すなふきんぬさん、Hongliangさん 返信有難うございます。

すいません、戻しのところを書いていませんでした。
改めて確認していたところ、正常に画像を見ることができました。次のコードになります。
しかし疑問点が残りました。

// Capture
Rectangle myRectangle = Screen.PrimaryScreen.Bounds;
Bitmap bmp = new Bitmap(myRectangle.Width, myRectangle.Height, PixelFormat.Format24bppRgb);
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(myRectangle.X, myRectangle.Y, 0, 0, myRectangle.Size, CopyPixelOperation.SourceCopy);
g.InterpolationMode = InterpolationMode.Default;
}

// Bitmap->byte[]
BitmapData bitd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
IntPtr scan0 = bitd.Scan0;
byte[] pixels = new byte[bitd.Height * bitd.Width * 3];
Marshal.Copy(scan0, pixels, 0, pixels.Length);
bmp.UnlockBits(bitd);

// byte[]->Bitmap
Bitmap bmp2 = new Bitmap(myRectangle.Width, myRectangle.Height, PixelFormat.Format24bppRgb);
BitmapData bitd2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
Marshal.Copy(pixels, 0, bitd2.Scan0, pixels.Length);
bmp2.UnlockBits(bitd2);

bmp2.Save("C:\\test.bmp", ImageFormat.Bmp);

ここで、Bitmap->byte[]の箇所について。
byte[] pixels = new byte[bitd.Height * bitd.Width * 3];
の*3はどのような意味があるのでしょうか。
これが無いことで、Save後に画像データを参照すると、3分の2ほど欠落していました。
返信 編集キー/


管理者用

- Child Tree -