Click here to Skip to main content
15,896,522 members
Home / Discussions / C#
   

C#

 
GeneralOdbcDataReader!! Pin
trk_wakil26-May-05 5:04
trk_wakil26-May-05 5:04 
GeneralRe: OdbcDataReader!! Pin
pubududilena26-May-05 19:34
pubududilena26-May-05 19:34 
Generalsocket handling Pin
cyrus10926-May-05 4:49
cyrus10926-May-05 4:49 
GeneralRe: socket handling Pin
S. Senthil Kumar26-May-05 5:06
S. Senthil Kumar26-May-05 5:06 
GeneralRe: socket handling Pin
cyrus10926-May-05 5:12
cyrus10926-May-05 5:12 
GeneralRe: socket handling Pin
S. Senthil Kumar26-May-05 6:19
S. Senthil Kumar26-May-05 6:19 
GeneralRe: socket handling Pin
cyrus10926-May-05 10:17
cyrus10926-May-05 10:17 
GeneralHandling Win32 logical palette's in C# Pin
Rahsas26-May-05 3:41
Rahsas26-May-05 3:41 
I'm having a hard time handling logical palette's in C#. What I'm trying to do, is extract a palette from a 256 color bitmap, place it into a logical palette, select the logical palette as the current system palette, realize the new palette, and then draw the 256 color bitmap to the screen using the colors in it's palette (which is now the current system palette). Everything seems to be working great up to the point where I start drawing the bitmap. When I'm running the desktop in 256 color mode, it will only draw the bitmap in 16 colors (Windows standard colors) even though the palette is selected and realized correctly. I've included the code I've used to implement this. If anyone knows how to get this to work correctly, let me know. Thanks in advance! Anyway, here is the code I'm using.

<br />
IntPtr hWindowDC = IntPtr.Zero;<br />
<br />
public class User<br />
{<br />
     [DllImport("user32.dll")]<br />
     public static extern IntPtr GetDC(IntPtr hWnd);<br />
<br />
     [DllImport("user32.dll")]<br />
     public static extern void ReleaseDC(IntPtr hWnd, IntPtr hDC);<br />
}<br />
<br />
public class Gdi<br />
{<br />
     [DllImport("gdi32.dll")]<br />
     public static extern IntPtr SelectPalette(IntPtr hDC, IntPtr hPalette, bool bForceBackground);<br />
<br />
     [DllImport("gdi32.dll")]<br />
     public static extern int RealizePalette(IntPtr hDC);<br />
<br />
     [DllImport("gdi32.dll")]<br />
     public static extern int GetDeviceCaps(IntPtr hDC, int nIndex);<br />
<br />
     [DllImport("gdi32.dll")]<br />
     public static extern IntPtr CreatePalette([In] IntPtr lpLogPalette);<br />
<br />
     public const int PC_RESERVED = 0x01;     // palette index used for animation<br />
     public const int PC_EXPLICIT = 0x02;     // palette index is explicit to device<br />
     public const int PC_NOCOLLAPSE = 0x04;     // do not match color to system palette<br />
}<br />
<br />
[StructLayout(LayoutKind.Sequential)]<br />
public struct LOGPALETTE<br />
{<br />
     public ushort palVersion;<br />
     public ushort palNumEntries;<br />
     public PALETTEENTRY palPalEntry;<br />
<br />
     public LOGPALETTE(ushort entries)<br />
     {<br />
          this.palVersion = 0x300;<br />
          this.palNumEntries = entries;<br />
          this.palPalEntry = new PALETTEENTRY(0,0,0,0);<br />
     }<br />
          <br />
     public int Length<br />
     {<br />
          get<br />
          {<br />
               return Marshal.SizeOf(palVersion) + Marshal.SizeOf(palNumEntries) + (this.palNumEntries * 4);<br />
          }<br />
     }<br />
}<br />
<br />
[StructLayout(LayoutKind.Sequential)]<br />
public struct PALETTEENTRY<br />
{<br />
     public byte peRed;<br />
     public byte peGreen;<br />
     public byte peBlue;<br />
     public byte peFlags;<br />
<br />
     public PALETTEENTRY(byte red, byte green, byte blue, byte flags)<br />
     {<br />
          this.peRed = red;<br />
          this.peGreen = green;<br />
          this.peBlue = blue;<br />
          this.peFlags = flags;<br />
     }<br />
<br />
     public override string ToString()<br />
     {<br />
          return string.Format("{0} {1} {2}", Convert.ToString(peRed, 10), Convert.ToString(peGreen, 10), Convert.ToString(peBlue, 10));<br />
     }<br />
<br />
}<br />
<br />
<br />
private static void LoadImagePalette()<br />
{<br />
     Assembly exeAssembly = Assembly.GetExecutingAssembly();<br />
<br />
     Stream imgStream = null;<br />
<br />
     imgStream = exeAssembly.GetManifestResourceStream("MyPalette.image256.bmp");<br />
     if( !(null==imgStream) )<br />
     {<br />
          Image image = Bitmap.FromStream( imgStream );// as Bitmap;<br />
          LOGPALETTE logPal = new LOGPALETTE((ushort)(image.Palette.Entries.Length));<br />
<br />
          byte []paletteBytes = new byte[image.Palette.Entries.Length*4];<br />
<br />
          for (int i=0; i < image.Palette.Entries.Length; i++)<br />
          {<br />
               paletteBytes[(i*4)] = image.Palette.Entries[i].R; // red<br />
               paletteBytes[(i*4)+1] = image.Palette.Entries[i].G; // green<br />
               paletteBytes[(i*4)+2] = image.Palette.Entries[i].B; // blue<br />
               paletteBytes[(i*4)+3] = 0; // flags<br />
          }<br />
<br />
          IntPtr ptrPal = Marshal.AllocHGlobal(logPal.Length);<br />
          Marshal.StructureToPtr(logPal, ptrPal, true);<br />
<br />
          IntPtr palPalEntryAddress = new IntPtr(ptrPal.ToInt32() + Marshal.SizeOf(logPal.palVersion) + Marshal.SizeOf(logPal.palNumEntries) );<br />
          Marshal.Copy(paletteBytes, 0, palPalEntryAddress, paletteBytes.Length);<br />
                    <br />
          IntPtr hPalette = Gdi.CreatePalette(ptrPal);<br />
<br />
          Gdi.SelectPalette(hWindowDC, hPalette, false);//false);<br />
          Gdi.RealizePalette(hWindowDC);<br />
<br />
          Graphics newGraphics = Graphics.FromHdc(hWindowDC);<br />
          newGraphics.DrawImage(image, 0, 0);<br />
<br />
          if (ptrPal != IntPtr.Zero)<br />
               Marshal.FreeHGlobal(ptrPal);<br />
                    <br />
          imgStream.Close();<br />
<br />
     }<br />
}<br />

GeneralWord automation selecting tray Pin
Master_Blaster26-May-05 3:05
Master_Blaster26-May-05 3:05 
GeneralRemoting Q. Pin
Member 199247026-May-05 1:06
Member 199247026-May-05 1:06 
GeneralRe: Remoting Q. Pin
S. Senthil Kumar26-May-05 2:59
S. Senthil Kumar26-May-05 2:59 
GeneralRe: Remoting Q. Pin
Member 199247026-May-05 4:42
Member 199247026-May-05 4:42 
GeneralImplementation in C# Pin
Diego12326-May-05 0:52
Diego12326-May-05 0:52 
GeneralRe: Implementation in C# Pin
Marc Clifton26-May-05 1:04
mvaMarc Clifton26-May-05 1:04 
GeneralRe: Implementation in C# Pin
Colin Angus Mackay26-May-05 1:49
Colin Angus Mackay26-May-05 1:49 
GeneralRe: Implementation in C# Pin
S. Senthil Kumar26-May-05 2:55
S. Senthil Kumar26-May-05 2:55 
GeneralRe: Implementation in C# Pin
Colin Angus Mackay26-May-05 3:16
Colin Angus Mackay26-May-05 3:16 
GeneralRe: Implementation in C# Pin
S. Senthil Kumar26-May-05 4:24
S. Senthil Kumar26-May-05 4:24 
GeneralWhy toolbar buttons doesn't care of CausesValidation Pin
Itanium26-May-05 0:38
Itanium26-May-05 0:38 
GeneralRe: Why toolbar buttons doesn't care of CausesValidation Pin
Luis Alonso Ramos26-May-05 5:12
Luis Alonso Ramos26-May-05 5:12 
GeneralRe: Why toolbar buttons doesn't care of CausesValidation Pin
Luis Alonso Ramos26-May-05 5:13
Luis Alonso Ramos26-May-05 5:13 
GeneralRe: Why toolbar buttons doesn't care of CausesValidation Pin
Itanium31-May-05 22:42
Itanium31-May-05 22:42 
GeneralRe: Why toolbar buttons doesn't care of CausesValidation Pin
Luis Alonso Ramos1-Jun-05 6:01
Luis Alonso Ramos1-Jun-05 6:01 
Generaldatagrid complex object binding Pin
nonick226-May-05 0:09
nonick226-May-05 0:09 
GeneralDisplay line and column on satusbar Pin
hoangsamac25-May-05 23:45
hoangsamac25-May-05 23:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.