Windows Develop Bookmark and Share   
 index > Windows Forms General > Icon codec
 

Icon codec

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);

}

}

}

 

Jordi Serres  Thursday, January 12, 2006 9:21 PM

This is because the icon format is lost when you add the icon as a resource. I would not recommend adding icons as resources for this reason.

 

-mark

Program Manager

Microsoft

This post is provided "as-is"

 

Mark Rideout  Thursday, January 12, 2006 10:02 PM

Mark: I think you got it the other way round:

The problem is that the code does work when the image is loaded from a resource, and it doesn't when loaded from the database.

In the other hand, the software I'm developing needs to use a SQL database as storage for the images, which can be any type. In fact, the problem I come agross, shows only with icons, and works fine with, at least, bitmaps or jpg files.

Thanks anyway for your reply.

Jordi Serres  Friday, January 13, 2006 8:13 AM

This is because the icon format is lost when you add the icon as a resource. I would not recommend adding icons as resources for this reason.

 

-mark

Program Manager

Microsoft

This post is provided "as-is"

 

Mark Rideout  Thursday, January 12, 2006 10:02 PM

Mark: I think you got it the other way round:

The problem is that the code does work when the image is loaded from a resource, and it doesn't when loaded from the database.

In the other hand, the software I'm developing needs to use a SQL database as storage for the images, which can be any type. In fact, the problem I come agross, shows only with icons, and works fine with, at least, bitmaps or jpg files.

Thanks anyway for your reply.

Jordi Serres  Friday, January 13, 2006 8:13 AM

I have the same issue.

When trying to load an .ico from file and convert it to a byte array for SQL storage the save method throws an exception indication that the ImageCodecInfo parameter is Null when I called the method using the Save(Stream, ImageFormat) method. Apparently internally its trying to obtain the codec info and fails to find a suitable one. Using any other image format works great....

Is there an .ico codec that can be used touse this file format?

Guillermo Leal-Collazo  Monday, January 14, 2008 10:03 PM

You can use google to search for other answers

Custom Search

More Threads

• GDI Drawing On Top Of A Windows Form And All Its Controls
• Drag Drop Multiple Items from Between List Boxes
• Visual Basic 2003 - SendMessage Related
• Case sensitive Spell Check Component for .NET
• Custom List type widget
• set the visibility of a tabPage to true & false
• Range Validation in a PropertyGrid
• Changing the cursor
• adding unknown numvers of SubItems to ListView
• Dispose function