Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting byte[] into an Image object
#1
I'm trying my hand out with Visual C# and going into the realm of programming applications. Since I luv editing images and screwing around with them and whatnot, I would like to be able to "create" new images and be able to preview them in a window. Previously with Java, I would just modify the byte array however I needed to then write it into a PNG file, then look at it from there.

Here's what I'm attempting to do:
  1. Take byte arrays representing images from source files. NO EXCEPTIONS!! OR SO HELP ME GAWD!!
  2. Apply various modifications to the byte arrays to form new images/composite images.
  3. Preview the results WITHOUT needing to writing to a file on the hard drive then referencing it.

There's the additional problem of telling the computer that the images have alpha as well. How would one go about doing this?
Reply
#2
KajitiSouls Wrote:I'm trying my hand out with Visual C# and going into the realm of programming applications. Since I luv editing images and screwing around with them and whatnot, I would like to be able to "create" new images and be able to preview them in a window. Previously with Java, I would just modify the byte array however I needed to then write it into a PNG file, then look at it from there.

Here's what I'm attempting to do:
  1. Take byte arrays representing images from source files. NO EXCEPTIONS!! OR SO HELP ME GAWD!!
  2. Apply various modifications to the byte arrays to form new images/composite images.
  3. Preview the results WITHOUT needing to writing to a file on the hard drive then referencing it.

There's the additional problem of telling the computer that the images have alpha as well. How would one go about doing this?

eh...if I understand you...
private Image BytesToImage(byte[] source)
{
Stream stream = new MemoryStream(source);
return Image.FromStream(stream);
}

As for actually modifying the image, I'd recommend using the GDI instead of modifying the raw bytes. If you do want to gain low level memory access to the bitmap data you can use the LockBits(...) function.
Reply
#3
Well, first of all, you have to parse the data given to you. I've worked with apngs before, so I know its documentation well (if you need that, that is). However, for static pictures, there should be some documentation on how to read that into a byte-array, write, concatenate and other stuff like that. Heck, it's even possible in Java, it should be possible in C#.

For 3: there should be some library to do that as well (I know, because Java offers to read a byte array to an image, and then print it as a picture)

But might I ask why you would like to do that without making a new temp file?
Reply
#4
@ Kortestanov: How exactly would C# know that the image has alphas? Just tell it it's a PNG or something?

I'll get to tinkering around over the next few days.

Devil's Sunrise Wrote:Well, first of all, you have to parse the data given to you. I've worked with apngs before, so I know its documentation well (if you need that, that is). However, for static pictures, there should be some documentation on how to read that into a byte-array, write, concatenate and other stuff like that. Heck, it's even possible in Java, it should be possible in C#.

For 3: there should be some library to do that as well (I know, because Java offers to read a byte array to an image, and then print it as a picture)

But might I ask why you would like to do that without making a new temp file?

I'm good with APNGs ty. I'm not exactly going to play into that this time xD I would like to ask if Firefox has fixed the issue with the frame offsets though, as I couldn't get those to work no matter what I did (unless they were 0,0). Also, I've never really played with application windows in Java, and I'm not attracted to what UIs I've seen.

As for the temp file thing, it's not a strict requirement, but it's something I would rather prefer not to do. Y'know, just in case I do something stupid. Haha xD I also think it gives less chance for a CRC error to show up, since I suspect that my lappy's HD has bad sectors in it. Nasty shyt happens whenever I run into that x_X
Reply
#5
KajitiSouls Wrote:@ Kortestanov: How exactly would C# know that the image has alphas? Just tell it it's a PNG or something?

I'll get to tinkering around over the next few days.
The "Image" or "Bitmap" object automatically supports alpha (it's not a real alpha, though. You'll notice if you try working with clipboard and backgrounds). If the bytes you are supplying contain alpha information, it will automatically be parsed.
Reply
#6
Kortestanov Wrote:The "Image" or "Bitmap" object automatically supports alpha (it's not a real alpha, though. You'll notice if you try working with clipboard and backgrounds). If the bytes you are supplying contain alpha information, it will automatically be parsed.

That's going to be a problem. I assume ReadAllBytes will fix that?
Reply
#7
KajitiSouls Wrote:That's going to be a problem. I assume ReadAllBytes will fix that?
Theres no difference between using Image.FromFile or Image.FromStream using bytes read by ReadAllBytes.
The Bitmap object has a very weird way to handle transparency... Instead of properly implementing alpha channeling in the controls\forms, Microsoft made the transparent pixels be changed to the background color of your form. This is internal only, though, and if you browse the pixels using GetPixel it will look like they really are transparent. It will also save the picture correctly to file. However, if you try writing the Bitmap object to the clipboard using Clipboard.SetImage, the image set will have it's transparent pixels replaced with the background color. Another way to see this bug is, if you take a textBox control, and place a pictureBox over it containing a transparent image the textBox will not be seen through the supposedly transparent image, because it was actually replaced with a bitmap containing your form's background color.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)