Re: 8 bit image
- From: "Michael Phillips, Jr." <mphillips53@xxxxxxxxxxxxxxx>
- Date: Tue, 22 Nov 2005 10:44:48 -0500
Reverse the following lines to correct the sample code:
>e.Graphics.ReleaseHdc(hdc);
> SelectPalette(hdc, holdPal, false );
To:
SelectPalette(hdc, holdPal, false );
e.Graphics.ReleaseHdc(hdc);
"Michael Phillips, Jr." <mphillips53@xxxxxxxxxxxxxxx> wrote in message
news:OgXaKs37FHA.3976@xxxxxxxxxxxxxxxxxxxxxxx
> Here you go. Please be advised this sample
> does no error checking and was designed for
> a 8bpp bitmap only!
>
> // csharp code paste into form code window
>
> using System;
> using System.Drawing;
> using System.Collections;
> using System.ComponentModel;
> using System.Windows.Forms;
> using System.Data;
> using System.IO;
> using System.Runtime.InteropServices;
>
> namespace bitmap
> {
> /// <summary>
> /// Summary description for Form1.
> /// </summary>
> public class Form1 : System.Windows.Forms.Form
> {
> /// <summary>
> /// Required designer variable.
> /// </summary>
> private System.ComponentModel.Container components = null;
> public const uint DIB_RGB_COLORS = 0;
>
> [StructLayout(LayoutKind.Sequential)]
> public struct RGBQUAD
> {
> public byte rgbBlue;
> public byte rgbGreen;
> public byte rgbRed;
> public byte rgbReserved;
> };
>
> [StructLayout(LayoutKind.Explicit)]
> public struct LOGPALETTE
> {
> [FieldOffset(0)] public ushort palVersion;
> [FieldOffset(2)] public ushort palNumEntries;
> [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
> SizeConst=1024)]
> [FieldOffset(4)] public byte[] palPalEntry;
> };
>
> [StructLayout(LayoutKind.Explicit)]
> public struct BITMAPFILEHEADER
> {
> [FieldOffset(0)] public ushort bfType;
> [FieldOffset(2)]public uint bfSize;
> [FieldOffset(6)]public ushort bfReserved1;
> [FieldOffset(8)]public ushort bfReserved2;
> [FieldOffset(10)]public uint bfOffBits;
> };
>
> [StructLayout(LayoutKind.Explicit)]
> public struct BITMAPINFOHEADER
> {
> [FieldOffset(0)]public uint biSize;
> [FieldOffset(4)] public int biWidth;
> [FieldOffset(8)]public int biHeight;
> [FieldOffset(12)]public ushort biPlanes;
> [FieldOffset(14)]public ushort biBitCount;
> [FieldOffset(16)]public uint biCompression;
> [FieldOffset(20)]public uint biSizeImage;
> [FieldOffset(24)]public int biXPelsPerMeter;
> [FieldOffset(28)]public int biYPelsPerMeter;
> [FieldOffset(32)]public uint biClrUsed;
> [FieldOffset(36)]public uint biClrImportant;
> }
>
> [StructLayout(LayoutKind.Explicit)]
> public struct BITMAPINFO
> {
> [FieldOffset(0)]public BITMAPINFOHEADER bmiHeader;
> [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
> SizeConst=1024)]
> [FieldOffset(40)]public byte[] bmiColors;
> };
>
> public enum TernaryRasterOperations
> {
> SRCCOPY = 0x00CC0020, // dest = source
> SRCPAINT = 0x00EE0086, // dest = source OR dest
> SRCAND = 0x008800C6, // dest = source AND dest
> SRCINVERT = 0x00660046, // dest = source XOR dest
> SRCERASE = 0x00440328, // dest = source AND (NOT dest)
> NOTSRCCOPY = 0x00330008, // dest = (NOT source)
> NOTSRCERASE = 0x001100A6, // dest = (NOT src) AND (NOT dest)
> MERGECOPY = 0x00C000CA, // dest = (source AND pattern)
> MERGEPAINT = 0x00BB0226, // dest = (NOT source) OR dest
> PATCOPY = 0x00F00021, // dest = pattern
> PATPAINT = 0x00FB0A09, // dest = DPSnoo
> PATINVERT = 0x005A0049, // dest = pattern XOR dest
> DSTINVERT = 0x00550009, // dest = (NOT dest)
> BLACKNESS = 0x00000042, // dest = BLACK
> WHITENESS = 0x00FF0062, // dest = WHITE
> };
>
> [DllImport("gdi32.dll")]
> public static extern bool DeleteObject(IntPtr hObject);
>
> [DllImport("gdi32.dll")]
> public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hBitmap);
>
> [DllImport("gdi32.dll")]
> public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest,
> int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc,
> TernaryRasterOperations dwRop );
>
> [DllImport("gdi32.dll")]
> public static extern IntPtr CreateCompatibleDC( IntPtr hdc );
>
> [DllImport("gdi32.dll")]
> public static extern bool DeleteDC( IntPtr hdc );
>
> [DllImport("gdi32.dll")]
> public static extern int SetBkMode( IntPtr hdc, int iBkMode );
>
> [DllImport("gdi32.dll")]
> public static extern int SetBkColor( IntPtr hdc, int crColor );
>
> [DllImport("gdi32.dll")]
> public static extern int SetTextColor( IntPtr hdc, int crColor );
>
> [DllImport("user32.dll")]
> public static extern IntPtr GetDC( IntPtr hWnd );
>
> [DllImport("user32.dll")]
> public static extern bool ReleaseDC( IntPtr hWnd, IntPtr hdc );
>
> [DllImport("gdi32.dll")]
> static extern IntPtr CreateDIBSection(IntPtr hdc, [In] ref BITMAPINFO
> pbmi,
> uint iUsage, [In][Out] ref IntPtr ppvBits, IntPtr hSection, uint
> dwOffset);
>
> [DllImport("gdi32.dll")]
> static extern IntPtr CreatePalette([In] ref LOGPALETTE lplgpl);
>
> [DllImport("gdi32.dll")]
> static extern IntPtr SelectPalette(IntPtr hdc, IntPtr hpal,
> bool bForceBackground);
>
> [DllImport("gdi32.dll")]
> static extern uint RealizePalette(IntPtr hdc);
>
> // gdi variables
> IntPtr hBitmap;
> IntPtr hMemDC;
> IntPtr hOldBitmap;
> IntPtr hPalette;
>
> public Form1()
> {
> //
> // Required for Windows Form Designer support
> //
> InitializeComponent();
>
> //
> // TODO: Add any constructor code after InitializeComponent call
> //
> }
>
> /// <summary>
> /// Clean up any resources being used.
> /// </summary>
> protected override void Dispose( bool disposing )
> {
> if( disposing )
> {
> if (components != null)
> {
> components.Dispose();
> }
> }
> base.Dispose( disposing );
> }
>
> #region Windows Form Designer generated code
> /// <summary>
> /// Required method for Designer support - do not modify
> /// the contents of this method with the code editor.
> /// </summary>
> private void InitializeComponent()
> {
> //
> // Form1
> //
> this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
> this.ClientSize = new System.Drawing.Size(292, 266);
> this.Name = "Form1";
> this.Text = "Form1";
> this.Load += new System.EventHandler(this.Form1_Load);
> this.Paint += new
> System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
> this.Leave += new System.EventHandler(this.Form1_Leave);
>
> }
> #endregion
>
> /// <summary>
> /// The main entry point for the application.
> /// </summary>
> [STAThread]
> static void Main()
> {
> Application.Run(new Form1());
> }
>
> private void Form1_Load(object sender, System.EventArgs e)
> {
> FileStream fs = new FileStream(@"c:\\testimages\\8bbpIndexed.bmp",
> FileMode.Open, FileAccess.Read);
>
> // read file header
> int cbRead = 0;
> BITMAPFILEHEADER bf = new BITMAPFILEHEADER();
> byte [] buff = new byte [14];
> cbRead = fs.Read(buff, 0, 14);
> IntPtr ptr = Marshal.AllocHGlobal(buff.Length);
> Marshal.Copy(buff, 0x0, ptr, buff.Length);
> bf = (BITMAPFILEHEADER)Marshal.PtrToStructure( ptr,
> typeof(BITMAPFILEHEADER));
> Marshal.FreeHGlobal(ptr);
> // read bitmapinfo
> fs.Seek( 14, SeekOrigin.Begin );
> BITMAPINFO bi = new BITMAPINFO();
> buff = new byte [Marshal.SizeOf(bi.bmiHeader) + 1024];
> cbRead = fs.Read(buff, 0, Marshal.SizeOf(bi.bmiHeader) + 1024);
> ptr = Marshal.AllocHGlobal(buff.Length);
> Marshal.Copy(buff, 0x0, ptr, buff.Length);
> bi = (BITMAPINFO)Marshal.PtrToStructure( ptr, typeof(BITMAPINFO));
> Marshal.FreeHGlobal(ptr);
> // read the dib bits
> long offset = fs.Seek( bf.bfOffBits, SeekOrigin.Begin );
> buff = new byte [ bi.bmiHeader.biSizeImage ];
> cbRead = fs.Read(buff, 0, buff.Length);
>
> IntPtr hDC = GetDC( IntPtr.Zero );
> this.hMemDC = CreateCompatibleDC(hDC);
> IntPtr pBits = IntPtr.Zero;
> this.hBitmap = CreateDIBSection( this.hMemDC, ref bi, DIB_RGB_COLORS,
> ref pBits, (IntPtr)null, 0 );
> ReleaseDC( (IntPtr)null, hDC );
> Marshal.Copy(buff, 0x0, pBits, buff.Length);
> this.hOldBitmap = SelectObject(hMemDC, hBitmap );
> // close file stream
> fs.Close();
>
> // create palette
> LOGPALETTE logpal = new LOGPALETTE();
> logpal.palVersion = 0x300;
> logpal.palNumEntries = 1024;
> logpal.palPalEntry = new byte[1024];
>
> for ( int i = 0; i < 1024; i++ )
> logpal.palPalEntry[i] = bi.bmiColors[i];
>
> this.hPalette = CreatePalette( ref logpal );
>
> // resize the form
> this.Width = bi.bmiHeader.biWidth;
> this.Height = bi.bmiHeader.biHeight;
> }
>
> private void Form1_Leave(object sender, System.EventArgs e)
> {
> // clean up gdi objects
> SelectObject(this.hMemDC, this.hOldBitmap );
> DeleteDC(this.hMemDC);
> DeleteObject(this.hBitmap);
> DeleteObject(this.hPalette);
> }
>
> private void Form1_Paint(object sender,
> System.Windows.Forms.PaintEventArgs e)
> {
> IntPtr hdc = e.Graphics.GetHdc();
>
> // select palette into device
> IntPtr holdPal = SelectPalette( hdc, this.hPalette, false );
> RealizePalette( hdc);
>
> bool bDrawn = BitBlt(hdc, 0, 0, this.Width, this.Height, hMemDC, 0, 0,
> TernaryRasterOperations.SRCCOPY);
> e.Graphics.ReleaseHdc(hdc);
>
> SelectPalette(hdc, holdPal, false );
> }
> }
> }
>
> // end of csharp code
>
.
- Follow-Ups:
- Re: 8 bit image
- From: grstefan
- Re: 8 bit image
- References:
- Re: 8 bit image
- From: Michael Phillips, Jr.
- Re: 8 bit image
- From: grstefan
- Re: 8 bit image
- From: Michael Phillips, Jr.
- Re: 8 bit image
- Prev by Date: Re: 8 bit image
- Next by Date: Re: Scaling an image and writing text on it
- Previous by thread: Re: 8 bit image
- Next by thread: Re: 8 bit image
- Index(es):
Relevant Pages
|