How to get image bits from a Bitmap

  • Thread starter Thread starter Frank
  • Start date Start date
F

Frank

Given a bitmap I want to write a Icon file using it.

I believe I can do it except for writing the bits of the image.

Can you tell me how to get the bits into a ByteArray



Thanks
 
System.Drawing will allow you to read an icon but you will not be able to
write an icon as there is no encoder for icons.

If you want to write your own icon encoder, you need to create a bitmap
representing the AND mask and a bitmap representing the XOR mask.

Here is the .ico format:
http://www.martinreddy.net/gfx/2d/BMP.txt

If you want to access the bits, use LockBits with the Marshal class.

Here you will find sample code in VB for LockBits and the Marshal class for
copying to a byte array:
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
 
thank, you filled at least the rest of today


Michael Phillips said:
System.Drawing will allow you to read an icon but you will not be able to
write an icon as there is no encoder for icons.

If you want to write your own icon encoder, you need to create a bitmap
representing the AND mask and a bitmap representing the XOR mask.

Here is the .ico format:
http://www.martinreddy.net/gfx/2d/BMP.txt

If you want to access the bits, use LockBits with the Marshal class.

Here you will find sample code in VB for LockBits and the Marshal class
for copying to a byte array:
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
 
Maybe I missed something but all I didn't see anything in the example that
related to saving a bitmap in an icon
 
Frank said:
Maybe I missed something but all I didn't see anything in the
example that related to saving a bitmap in an icon

I thought you have a bitmap and you want to make an Icon from the Bitmap,
then save the Icon as a file.

Armin
 
Armin Zingler said:
I thought you have a bitmap and you want to make an Icon from the Bitmap,
then save the Icon as a file.

I did.
I should of said "bitmap as an icon file"
I just looked at the site again and I see what you were getting at.
I can create an Icon from the bitmap.
Now I need to find out how to write it.
I know the format and was focused on building the data in menory and then
writing it. But I didn't hnow how to get the inage data from the bitmap.
That's what I thought you were helping with. Actually I see now it is more
compicated then just copying the image data from the bitmap.

Given that I get a Icon from the bitmap do I serialize it to write it? (I
know only the word serialize - never used it.)

Thanks
 
Frank said:
I did.
I should of said "bitmap as an icon file"
I just looked at the site again and I see what you were getting at.
I can create an Icon from the bitmap.
Now I need to find out how to write it.
I know the format and was focused on building the data in menory and
then writing it. But I didn't hnow how to get the inage data from
the bitmap. That's what I thought you were helping with. Actually I
see now it is more compicated then just copying the image data from
the bitmap.

Given that I get a Icon from the bitmap do I serialize it to write
it? (I know only the word serialize - never used it.)


Ok, you do have an Icon now, did I get this right? Then call the Icon's Save
method.


Armin
 
Armin Zingler said:
Ok, you do have an Icon now, did I get this right? Then call the Icon's
Save method.

Thanks for all the help.
I didn't find the right combination when I looked and figured I have to
write the file low level.
Guess I was influenced by an MS note I saw that said:
Bitmap.Save(..,Imaging.ImageFormat.Icon) wasn't available in GDI+
Q316563 says - GDI+ component of the .NET Framework does not have an encoder
that allows you to save files as WMF, EMF, or ICON files.
I wonder why not if all they had to do is:

Bitmap.GetHicon returns a handle to and Icon
Icon.FromHandle to get an Icon object
Icon.Save to write the file.

Maybe because - Icon.FromHandle() method demands
a.. SecurityPermission for access to unmanaged code. Related enumeration
UnmanagedCode.
means it can't be in G++. I don't know anything about this secutity stuff.

I'm going to try it now. Thanks again
 
Back
Top