A
aaron.radich
I'm testing the creating of a 20x10 4bpp Indexed bitmap on the fly.
The bitmap has a 16 color palette, which I manipulate before writing a
byte array back to the bitmap. I'm setting most of the bytes in the
byte array to the color black and only 1 byte/pixel to the color
purple. What's strange is that the final bitmap saved to disk has
purple virticle stripes running through it. Does anyone have any idea
what might be going on? I've included the full source for a simple
application that demonstrates the problem. Thanks in advance!
Aaron
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
byte[] aByteBuffer = new byte[200];
System.Drawing.Bitmap bmpBuffer = new Bitmap(@"C:
\SongbookMaster\Win32\BitmapStreamTest\WindowsFormsApplication1\bin
\Debug\Image1.bmp");
ColorPalette cpPalette = bmpBuffer.Palette;
cpPalette.Entries[0] = Color.Purple;
cpPalette.Entries[1] = Color.SkyBlue;
cpPalette.Entries[2] = Color.Yellow;
cpPalette.Entries[3] = Color.White;
cpPalette.Entries[4] = Color.Red;
cpPalette.Entries[5] = Color.Brown;
cpPalette.Entries[6] = Color.Black;
cpPalette.Entries[7] = Color.Black;
cpPalette.Entries[8] = Color.Black;
cpPalette.Entries[9] = Color.Black;
cpPalette.Entries[10] = Color.Black;
cpPalette.Entries[11] = Color.Black;
cpPalette.Entries[12] = Color.Black;
cpPalette.Entries[13] = Color.Black;
cpPalette.Entries[14] = Color.Black;
cpPalette.Entries[15] = Color.Black;
for (int i = 0; i < 200; i++)
{
aByteBuffer = 6;
}
aByteBuffer[0] = 0;
aByteBuffer[1] = 1;
aByteBuffer[2] = 2;
aByteBuffer[3] = 3;
aByteBuffer[4] = 4;
aByteBuffer[5] = 5;
bmpBuffer = CopyDataToBitmap(ref aByteBuffer, cpPalette);
string sBMFilename = @"C:\Temp\Bitmaps\TestBitmap.bmp";
if (File.Exists(sBMFilename))
File.Delete(sBMFilename);
bmpBuffer.Save(sBMFilename);
}
private Bitmap CopyDataToBitmap(ref byte[] data, ColorPalette
cpPalette)
{
// here create the Bitmap to the know height, width and
format
//Bitmap bmp = new Bitmap(iSCREEN_WIDTH, iSCREEN_HEIGHT,
PixelFormat.Format32bppArgb);
Bitmap bmp = new Bitmap(20, 10,
PixelFormat.Format4bppIndexed);
bmp.Palette = cpPalette;
// create a BitmapData and Lock all pixels to be written
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0,
bmp.Width, bmp.Height),
ImageLockMode.WriteOnly,
bmp.PixelFormat);
// copy the data from the byte array into BitmapData.Scan0
int iBytes = bmpData.Stride * bmp.Height;
//Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
Marshal.Copy(data, 0, bmpData.Scan0, iBytes);
// unlock the pixels
bmp.UnlockBits(bmpData);
bmpData = null;
// return the bitmap
return bmp;
}
}
}
The bitmap has a 16 color palette, which I manipulate before writing a
byte array back to the bitmap. I'm setting most of the bytes in the
byte array to the color black and only 1 byte/pixel to the color
purple. What's strange is that the final bitmap saved to disk has
purple virticle stripes running through it. Does anyone have any idea
what might be going on? I've included the full source for a simple
application that demonstrates the problem. Thanks in advance!
Aaron
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
byte[] aByteBuffer = new byte[200];
System.Drawing.Bitmap bmpBuffer = new Bitmap(@"C:
\SongbookMaster\Win32\BitmapStreamTest\WindowsFormsApplication1\bin
\Debug\Image1.bmp");
ColorPalette cpPalette = bmpBuffer.Palette;
cpPalette.Entries[0] = Color.Purple;
cpPalette.Entries[1] = Color.SkyBlue;
cpPalette.Entries[2] = Color.Yellow;
cpPalette.Entries[3] = Color.White;
cpPalette.Entries[4] = Color.Red;
cpPalette.Entries[5] = Color.Brown;
cpPalette.Entries[6] = Color.Black;
cpPalette.Entries[7] = Color.Black;
cpPalette.Entries[8] = Color.Black;
cpPalette.Entries[9] = Color.Black;
cpPalette.Entries[10] = Color.Black;
cpPalette.Entries[11] = Color.Black;
cpPalette.Entries[12] = Color.Black;
cpPalette.Entries[13] = Color.Black;
cpPalette.Entries[14] = Color.Black;
cpPalette.Entries[15] = Color.Black;
for (int i = 0; i < 200; i++)
{
aByteBuffer = 6;
}
aByteBuffer[0] = 0;
aByteBuffer[1] = 1;
aByteBuffer[2] = 2;
aByteBuffer[3] = 3;
aByteBuffer[4] = 4;
aByteBuffer[5] = 5;
bmpBuffer = CopyDataToBitmap(ref aByteBuffer, cpPalette);
string sBMFilename = @"C:\Temp\Bitmaps\TestBitmap.bmp";
if (File.Exists(sBMFilename))
File.Delete(sBMFilename);
bmpBuffer.Save(sBMFilename);
}
private Bitmap CopyDataToBitmap(ref byte[] data, ColorPalette
cpPalette)
{
// here create the Bitmap to the know height, width and
format
//Bitmap bmp = new Bitmap(iSCREEN_WIDTH, iSCREEN_HEIGHT,
PixelFormat.Format32bppArgb);
Bitmap bmp = new Bitmap(20, 10,
PixelFormat.Format4bppIndexed);
bmp.Palette = cpPalette;
// create a BitmapData and Lock all pixels to be written
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0,
bmp.Width, bmp.Height),
ImageLockMode.WriteOnly,
bmp.PixelFormat);
// copy the data from the byte array into BitmapData.Scan0
int iBytes = bmpData.Stride * bmp.Height;
//Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
Marshal.Copy(data, 0, bmpData.Scan0, iBytes);
// unlock the pixels
bmp.UnlockBits(bmpData);
bmpData = null;
// return the bitmap
return bmp;
}
}
}