PC Review


Reply
Thread Tools Rate Thread

Bitmap: From 32bppARGB to 8bppIndexed

 
 
RicercatoreSbadato
Guest
Posts: n/a
 
      22nd Sep 2005
I'm trying to use this code but something goes wrong. Any idea?

BitmapData oldData = img.LockBits(new Rectangle(0,0,width,height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

BitmapData newData = newBMP.LockBits(new Rectangle(0,0,width,height),
ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

int remain_old = oldData.Stride - width * 4;
int remain_new = newData.Stride - width * 1;

// unsafe block

unsafe
{
byte* currentPosition_Old = (byte*) oldData.Scan0;
byte* currentPosition_New = (byte*) newData.Scan0;

// Standard Deviation

for (int x=0; x<=width; x++)
{
for (int y=0; y<height; y++)
{
currentPosition_New[0] = (byte) currentPosition_Old[0];

currentPosition_Old += 4; // ARGB
currentPosition_New += 1; // indexed
}
currentPosition_Old += remain_old;
currentPosition_New += remain_new;
}
}
img.UnlockBits(oldData);
newBMP.UnlockBits(newData);

 
Reply With Quote
 
 
 
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      22nd Sep 2005
For details on how to access the data provided by LockBits see the article
in the GDI+ FAQ.

If you want to create an 8bpp indexed image from a 32 bpp one you can either
save the image as a GIF and hope that the spread palette is ok for your
purposes or create an optimised palette using the octtree quantization
method as described in this MSDN article:

http://msdn.microsoft.com/library/de...colorquant.asp

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





"RicercatoreSbadato" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm trying to use this code but something goes wrong. Any idea?
>
> BitmapData oldData = img.LockBits(new Rectangle(0,0,width,height),
> ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
>
> BitmapData newData = newBMP.LockBits(new Rectangle(0,0,width,height),
> ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
>
> int remain_old = oldData.Stride - width * 4;
> int remain_new = newData.Stride - width * 1;
>
> // unsafe block
>
> unsafe
> {
> byte* currentPosition_Old = (byte*) oldData.Scan0;
> byte* currentPosition_New = (byte*) newData.Scan0;
>
> // Standard Deviation
>
> for (int x=0; x<=width; x++)
> {
> for (int y=0; y<height; y++)
> {
> currentPosition_New[0] = (byte) currentPosition_Old[0];
>
> currentPosition_Old += 4; // ARGB
> currentPosition_New += 1; // indexed
> }
> currentPosition_Old += remain_old;
> currentPosition_New += remain_new;
> }
> }
> img.UnlockBits(oldData);
> newBMP.UnlockBits(newData);
>



 
Reply With Quote
 
RicercatoreSbadato
Guest
Posts: n/a
 
      22nd Sep 2005
I begin to read...
tnx

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting 8bpp raw data to a 32bppArgb Bitmap =?Utf-8?B?U2hhcm9u?= Microsoft C# .NET 3 1st Mar 2006 05:42 AM
Changing to 8bppIndexed question James dean Microsoft C# .NET 0 4th Aug 2005 10:54 AM
Extract portion of a bitmap to display, not entire bitmap. Alex Gray Microsoft C# .NET 2 22nd Feb 2004 01:42 PM
Question on how to access bitmap: Public Function getBitmap() As Bitmap Michael Murphy Microsoft VB .NET 2 7th Oct 2003 01:54 PM
extract OLE bitmap from table and save as a bitmap file Brett Rawcliffe Microsoft Access 1 19th Jul 2003 03:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:14 AM.