By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

I want to edit byte data from a .png file - increase byte value

P: 7
If I open a .png file either by
Dim BitmapImage As New Bitmap(filename)
OR
ByteArray = My.Computer.FileSystem.ReadAllBytes(filename)

I want to either edit byte values (one by one)
or copy data, byte by byte, in such a way that I can edit values during copy

I can then save file
BitmapImage.Save("D:\Test.png", Imaging.ImageFormat.Png)
##### Not sure how to save a ByteArray
Oct 15 '16 #1

✓ answered by IronRazer

Well, it depends on what exactly you want to change in the bytes of the image and how large the image is that will determine the best way to go about this.

If you just want to adjust the ARGB values to change the colors or transparency of a large png image like 256x256 or larger, then i suggest using the Bitmap.LockBits method at the link below. LockBits is very quick at processing large images. There is an example there to show a basic usage.
https://msdn.microsoft.com/en-us/lib...code-snippet-1

If you want to adjust the colors of smaller images, then you could use the Bitmap.GetPixel and Bitmap.SetPixel methods at the links below to read the Color of each pixel, and set the color of each pixel. These methods are slower than Lockbits which is why i only recommend using them on small images. Again, there are examples of their usage at the links.
https://msdn.microsoft.com/en-us/lib...vs.110%29.aspx
https://msdn.microsoft.com/en-us/lib...vs.110%29.aspx

Using the File.ReadAllBytes method to read the bytes of the image into a Byte array, adjusting the bytes, and then saving the Byte array back to the hard drive using the File.WriteAllBytes method would be pretty fast too.

However, you would need to understand how the bytes are arranged in the file so that you know what bytes you want to change. Reading the bytes like this will include the file header, image data, and other bytes such as the file ending byte. You can read the link below to read how the bytes of a Png file can be arranged.
https://en.wikipedia.org/wiki/Portable_Network_Graphics

Here are the msdn links for the File.ReadAllBytes and File.WriteAllBytes methods.
https://msdn.microsoft.com/en-us/lib...vs.110%29.aspx
https://msdn.microsoft.com/en-us/lib...vs.110%29.aspx

Share this Question
Share on Google+
4 Replies


IronRazer
P: 82
Well, it depends on what exactly you want to change in the bytes of the image and how large the image is that will determine the best way to go about this.

If you just want to adjust the ARGB values to change the colors or transparency of a large png image like 256x256 or larger, then i suggest using the Bitmap.LockBits method at the link below. LockBits is very quick at processing large images. There is an example there to show a basic usage.
https://msdn.microsoft.com/en-us/lib...code-snippet-1

If you want to adjust the colors of smaller images, then you could use the Bitmap.GetPixel and Bitmap.SetPixel methods at the links below to read the Color of each pixel, and set the color of each pixel. These methods are slower than Lockbits which is why i only recommend using them on small images. Again, there are examples of their usage at the links.
https://msdn.microsoft.com/en-us/lib...vs.110%29.aspx
https://msdn.microsoft.com/en-us/lib...vs.110%29.aspx

Using the File.ReadAllBytes method to read the bytes of the image into a Byte array, adjusting the bytes, and then saving the Byte array back to the hard drive using the File.WriteAllBytes method would be pretty fast too.

However, you would need to understand how the bytes are arranged in the file so that you know what bytes you want to change. Reading the bytes like this will include the file header, image data, and other bytes such as the file ending byte. You can read the link below to read how the bytes of a Png file can be arranged.
https://en.wikipedia.org/wiki/Portable_Network_Graphics

Here are the msdn links for the File.ReadAllBytes and File.WriteAllBytes methods.
https://msdn.microsoft.com/en-us/lib...vs.110%29.aspx
https://msdn.microsoft.com/en-us/lib...vs.110%29.aspx
Oct 16 '16 #2

P: 7
Thanks for your reply...

I looking at the links you've suggested - It'll take my awhile to play with the code.

To be more honest. I have a working steganography program which works perfectly on .bmp files. But who EVER sends pics in an email in bmp format?

So I am trying to adapt the working concept to use with png files.
Since png use a compression method that I do not understand I thought to read the picture data into an array so that I can access and modify the data as I did the bmp file.
Obviously, to reliably encode a message, I must be able to accurately set the individual RGB values and then resave as a new png file.
I think I can use the methods you outline - unless now that you know what I'm trying to do, you can suggest a better approach
Oct 16 '16 #3

IronRazer
P: 82
I really have no use for Stenography, i don`t really have anything to hide from anyone. 8)

However, a person i know from the Msdn VB.Net forum has posted an example of using an LSB (least significant bit) Stenography algorithm at the link below. I don`t approve of some of the methods he used in the example code but, it does work. Maybe it will give you some ideas.

https://social.msdn.microsoft.com/Fo...orum=vbgeneral

You can load a .Jpg, .Bmp, or .Png image, encode the image and text, then save them as .Png images.
Oct 16 '16 #4

P: 7
thanks again.. I'll look at the example.
Cheers
Peter
Oct 16 '16 #5

Post your reply

Sign in to post your reply or Sign up for a free account.