| ■No100284 (まさお さん) に返信
> プログラムを編集して試してみましたがうまくいきません。
> 下記プログラムでImageWidth が(4の倍数)か(4の倍数-1)の時はうまく動くのですがそれ以外の時にうまく動きません。
「うまく動きません」の内容が変わってます。
2箇所修正したら動きました。dataWidth って不要なのでは?
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
int left = 10;
for (int i = 0; i < 4; i++) {
int ImageWidth = 100 + i;
const int ImageHeight = 50;
int stride = (ImageWidth * 24 + 31) / 32 * 4;
int dataWidth = (ImageWidth + (stride / 3 - ImageWidth)) * 3;
//var scan0 = Enumerable.Repeat<byte>(0x00, (dataWidth) * ImageHeight).ToArray(); ★ここ
var scan0 = Enumerable.Repeat<byte>(0x00, stride * ImageHeight).ToArray();
// 青のグラデーションになるようなデータ作成
byte c;
for (int y = 0; y < ImageHeight; y++) {
c = 0;
for (int x = 0; x < ImageWidth * 3; x++) {
if (x % 3 == 0) {
//int index = y * (dataWidth) + x; ★ここ
int index = y * (stride) + x;
scan0[index] = c;
c += 2;
}
}
}
using (Bitmap bitmap = new Bitmap(ImageWidth, ImageHeight, PixelFormat.Format24bppRgb)) {
BitmapData bitmapData = bitmap.LockBits(
new Rectangle(0, 0, ImageWidth, ImageHeight),
ImageLockMode.WriteOnly,
PixelFormat.Format24bppRgb);
Marshal.Copy(scan0, 0, bitmapData.Scan0, scan0.Length);
bitmap.UnlockBits(bitmapData);
e.Graphics.DrawImage(bitmap, new Point(left, 10));
}
left += ImageWidth + 10;
}
}
|