SetDIBitsToDevice problem - reading RGB file
Hi,
I am trying to read an RGB File and display the result in a picturebox using SetDIBitsToDevice API (I know there is StretchDIBits API too, but right now I am focusing on one API and get to display the image), but no matter what I do, i end up with a runtime error of Invalid Argument on API Call. I am unable to calculate which parameter is wrong.
My Code so far
Code:
I am using this API on Paint event of picturebox
//ActualImageWidth = System.Drawing.Image.FromFile("test.rgb").Width;
//ActualImageHeight = System.Drawing.Image.FromFile("test.rgb").Height;
//string test;
//System.IO.StreamReader sr = new System.IO.StreamReader("test.rgb", Encoding.ASCII);
//test = sr.ReadToEnd();
//sr.Close();
byte[] data = System.IO.File.ReadAllBytes("test.rgb");
//System.IO.FileStream fs = System.IO.File.OpenRead("test.rgb");
//System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
//MessageBox.Show(br.ReadString());
//System.IO.MemoryStream ms = new System.IO.MemoryStream(data);
//Bitmap bmp = new Bitmap(ms);
//pictureBox1.Image = Image.FromHbitmap(bmp.GetHbitmap());
byte[] data3 = (new ReadImage(ref data)).ReadChannels();
PrintImage.BITMAPINFOHEADER bmiHeader = new PrintImage.BITMAPINFOHEADER();
bmiHeader.biSize = System.Runtime.InteropServices.Marshal.SizeOf(bmiHeader);
bmiHeader.biWidth = 400;
bmiHeader.biHeight = -400;
bmiHeader.biPlanes = 1;
bmiHeader.biBitCount = 24;
bmiHeader.biClrUsed = 0;
bmiHeader.biClrImportant = 0;
bmiHeader.biXPelsPerMeter = 0;
bmiHeader.biYPelsPerMeter = 0;
bmiHeader.biCompression = PrintImage.BI_RGB;
bmiHeader.biSizeImage = 400 * 400;
PrintImage.BITMAPINFO bmi = new PrintImage.BITMAPINFO(bmiHeader);
IntPtr lhDC = PrintImage.GetWindowDC(pictureBox1.Handle);
//Int16 hBMP;
//Int16 hDCMemory;
//IntPtr test = PrintImage.StretchDIBits(PrintImage.GetWindowDC(pictureBox1.Handle), 0, 0, (short)ActualImageWidth, (short)ActualImageHeight, 0, 0, (short)ActualImageWidth, (short)ActualImageHeight, data3, ref bmi, PrintImage.DIB_RGB_COLORS, PrintImage.SRCCOPY);
Int16 test = PrintImage.SetDIBitsToDevice(lhDC, 0, 100, 400, 400, 0, 0, 0, 400, data3, ref bmi, PrintImage.DIB_RGB_COLORS);
//if(PrintImage.GetDIBits(lhDC,
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
My Custom Class
Code:
public class ReadImage
{
private byte[] imageByteData;
public ReadImage(ref byte[] imageByteData)
{
this.imageByteData = imageByteData;
}
public byte[] ReadChannels()
{
byte[] channelData = new byte[imageByteData.Length * 3];
for (int count = 0; count < imageByteData.Length; ++count)
{
channelData[3 * count] = imageByteData[count];
channelData[3 * count + 1] = imageByteData[count];
channelData[3 * count + 2] = imageByteData[count];
}
return channelData;
}
}
and API is declared as
Code:
public struct PALETTEENTRY
{
public byte peRed;
public byte peGreen;
public byte peBlue;
public byte peFlags;
}
public struct BITMAPINFOHEADER
{
public Int32 biSize;
public Int16 biWidth;
public Int16 biHeight;
public Int16 biPlanes;
public Int16 biBitCount;
public Int32 biCompression;
public Int32 biSizeImage;
public Int16 biXPelsPerMeter;
public Int16 biYPelsPerMeter;
public Int32 biClrUsed;
public Int32 biClrImportant;
}
public struct BITMAPINFO
{
public BITMAPINFO(BITMAPINFOHEADER bmiHeader)
{
this.bmiHeader = bmiHeader;
bmiColors = new PALETTEENTRY[256];
}
public BITMAPINFOHEADER bmiHeader;
public PALETTEENTRY[] bmiColors;
}
public struct RECT
{
public Int16 Left;
public Int16 Top;
public Int16 Right;
public Int16 Bottom;
}
public const Int16 PIXELS = 3;
public const Int32 SRCCOPY = 0xCC0020;
public const Int16 BI_RGB = 0;
public const Int16 DIB_RGB_COLORS = 0;
public const Int16 GMEM_MOVEABLE = 2;
public const Int16 RASTERCAPS = 38;
public const Int16 RC_STRETCHDIB = 0x2000;
public const Int16 RC_PALETTE = 0x100;
public const Int16 PLANES = 14;
public const Int16 BITSPIXEL = 12;
public const Int16 SIZEPALETTE = 104;
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern Int16 SetDIBitsToDevice(IntPtr hDC, Int16 DestX, Int16 DestY, Int16 wDestWidth, Int16 wDestHeight, Int16 SrcX, Int16 SrcY, Int32 uStartScan, Int32 uScanLines, byte[] lpBits, ref BITMAPINFO BitsInfo, Int32 uColorUse);
I was provided with a sample in VC++ 6, and I tried to convert it into equivalent C# code.
Please need help urgent.
Actually I need to scale, rotate and apply Anti-Aliasing filter to the image. We were told to read the channels separately. Since, there is no header information present, I am unable to create a Bitmap calss of it.
Thank you