I've got a problem dealing with images. Icons in particular.
The situation is as follows: Create a form, create a PictureBox control and add an icon to it. The icon itself goes to the project resources.
Because the project I'm dealing with needs to save the images to the SQL Server database, it is necessary to get the image content, translate it into a byte[] before saving.
The problem arises when using the PictureBox.Save method: When the image comes from resources, everything is fine. Instead, when the picture comes from a file, something is not working.
I thought it might be something related to codecs, but now I'm confused.
Any Ideas?
using
System;
using
System.Drawing;
using
System.Windows.Forms;
namespace
Test
{
public
partial class Form1 : Form
{
public Form1()
{
System.Windows.Forms.PictureBox MyPictureBox;
// An icon has been previously saved in the project resources
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
MyPictureBox = new System.Windows.Forms.PictureBox();
MyPictureBox.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
System.IO.MemoryStream objMemoryStream0 = new System.IO.MemoryStream();
// this line executes as expected
MyPictureBox.Image.Save(objMemoryStream0, MyPictureBox.Image.RawFormat);
MyPictureBox = new System.Windows.Forms.PictureBox();
MyPictureBox.Image = System.Drawing.Image.FromFile(@"C:\Documents and Settings\jsr\My Documents\DotNet\UserInterface\Icons\00Folder1.ico");
System.IO.MemoryStream objMemoryStream1 = new System.IO.MemoryStream();
// ** Next line raises an exception **
MyPictureBox.Image.Save(objMemoryStream1, MyPictureBox.Image.RawFormat);
}
}
}