473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Bitmap: From 32bppARGB to 8bppIndexed

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);

Nov 17 '05 #1
2 5606
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" <em***********@yahoo.it> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
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);

Nov 17 '05 #2
I begin to read...
tnx ;)

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: James dean | last post by:
I take in a 32bpp bitmap at runtime and then i convert this to a gif using the bitmap save method.....is this an 8bppIndexed bitmap....it has a pallette and i can change the pixels.......so it must...
5
by: Lance | last post by:
I need to create a Drawing.Bitmap from an array of integer values. My current technique creates a bitmap that eventually becomes corrupt (i.e., the bitmap's pixels change to a different color...
2
by: active | last post by:
I find Bitmap.Save works for WMF files but Bitmap.FromFile does not. If I use FromFile on a WMF file that came with VS I get an exception. If I use it on a WMF file created with Bitmap.Save I...
8
by: Nathan Sokalski | last post by:
I am trying to write code to rotate a graphic that I have. Here is the code I am currently using: Dim frogbitmap As New Bitmap(Drawing.Image.FromFile(Server.MapPath("images/frog.gif"))) Dim...
3
by: Sharon | last post by:
I have a buffer of byte that contains a raw data of a 1 byte-per-pixel image data. I need to convert this buffer to a Bitmap of Format32bppArgb and to a Bitmap of Format24bppRgb. Can anybody...
7
by: Fir5tSight | last post by:
Hi All, I used the following code in C#: using System.Drawing; //blah blah blah Bitmap bmp = new Bitmap();
14
by: eliss.carmine | last post by:
I'm using TCP/IP to send a Bitmap object over Sockets. This is my first time using C# at all so I don't know if this is the "right" way to do it. I've already found out several times the way I was...
0
by: benfly08 | last post by:
Hi, guys. I have a program to draw bar/pie chart based on the data i hard coded in it. However, my image comes with "BLACK" background color. I don't know how to fix this. The code snippet is...
8
by: Joergen Bech | last post by:
Suppose I have Dim bm As New Bitmap(16, 16,Imaging.PixelFormat.Format8bppIndexed) I cannot use Dim g As Graphics = Graphics.FromImage(bmdest) Dim hdc As IntPtr = g.GetHdc() as the...
5
by: =?Utf-8?B?QVRU?= | last post by:
I have a bitmap of 100X100. On the load, the bitmap is created by a function (createimage()). On my OnPaint, I draw the image back to the screen (e.Graphics.DrawImage( bitmap, destrect)). Now,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.