Answered by:
Change color for Bitmap Image - No change in Hex string

Question
-
I have a Hex string that's coming from postscript file.
This Hex string is for the image - for which I need to modify RGB color values.
After modifying color values, I need to write the image back as Hex string in postscript file.
Below are the methods that I am using. All these methods are working fine including `UpdateColour(bitmapImage)`. I have confirmed it through debugging.
public static void ProcessImageColourMapping() { string imageDataSource = "803fe0503824160d0784426150b864361d0f8844625138a4562d178c466351b8e4763d1f904864523924964d27944a6552b964b65d2f984c665339a4d66d379c4e6753b9e4f67d3fa05068543a25168d47a4526954ba648202"; string imageDataSourceUpdated = GetUpdatedImage(imageDataSource); } public static string GetUpdatedImage(string strImageDataSource) { string imageDataSourceUpdated = ""; byte[] imageBytes = StringToByteArray(strImageDataSource); Bitmap bitmapImage = ByteArrayToBitmap(imageBytes); UpdateColour(bitmapImage); //I have confirmed by debugging that color values for pixel are actually updated for bitmapImage byte[] imageBytesUpdated = BitmapToByteArray(bitmapImage); imageDataSourceUpdated = ByteArrayToString(imageBytes); return imageDataSourceUpdated; } public static byte[] StringToByteArray(String imageHexString) { int numberOfChars = imageHexString.Length / 2; byte[] byteArray = new byte[numberOfChars]; using (var sr = new StringReader(imageHexString)) { for (int i = 0; i < numberOfChars; i++) byteArray[i] = Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16); } return byteArray; } public static Bitmap ByteArrayToBitmap(byte[] byteArray) { int width = 2144; //width and height are taken from postscript file for testing a single hex string. int height = 3; Bitmap bitmapImage = new Bitmap(width, height, PixelFormat.Format32bppPArgb); BitmapData bmpData = bitmapImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb); try { Marshal.Copy(byteArray, 0, bmpData.Scan0, byteArray.Length); } finally { bitmapImage.UnlockBits(bmpData); } return bitmapImage; } public static byte[] BitmapToByteArray(Bitmap bitmap) { BitmapData bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat); int numbytes = bmpdata.Stride * bitmap.Height; byte[] bytedata = new byte[numbytes]; try { Marshal.Copy(bmpdata.Scan0, bytedata, 0, numbytes); } finally { bitmap.UnlockBits(bmpdata); } return bytedata; } public static string ByteArrayToString(byte[] byteArray) { StringBuilder hex = new StringBuilder(byteArray.Length * 2); foreach (byte b in byteArray) { hex.AppendFormat("{0:x2}", b); } return hex.ToString(); } public static void UpdateColour(Bitmap bitmapImage) { for (int x = 0; x < bitmapImage.Width; x++) { for (int y = 0; y < bitmapImage.Height; y++) { Color color = Color.FromKnownColor(KnownColor.Aqua); //For Testing, change all pixels color to Color.Aqua bitmapImage.SetPixel(x, y, color); } } }
**Issue:**
Even though the color for all image pixels are changed:
bitmapImage.GetPixel(0,0) "{Name=50ffc8ff, ARGB=(80, 255, 200, 255)}" - BEFORE UpdateColor()
bitmapImage.GetPixel(0,0) "{Name=ff00ffff, ARGB=(255, 0, 255, 255)}" - AFTER UpdateColor()
If I compare the two hex strings:
`imageDataSource` and `imageDataSourceUpdated`, they are same :-(
Shouldn't they be different after I have modified underlying bitmap color values?
Can please guide what I am missing here.Monday, February 3, 2014 12:47 AM
Answers
-
Fix this line:
imageDataSourceUpdated = ByteArrayToString( imageBytesUpdated );
- Marked as answer by iniki Monday, February 3, 2014 1:53 PM
Monday, February 3, 2014 12:39 PM
All replies
-
Fix this line:
imageDataSourceUpdated = ByteArrayToString( imageBytesUpdated );
- Marked as answer by iniki Monday, February 3, 2014 1:53 PM
Monday, February 3, 2014 12:39 PM -
Wonder how could I miss this :-(
Now facing another issue, but going to post another question for that.
Monday, February 3, 2014 1:54 PM