Daniel Flannery
Daniel Flannery

Reputation: 1178

C# Argument 'picture' must be a picture that can be used as an Icon

I am having trouble importing an icon into my application. I have a main form and I am trying to import to it a new icon via the Icon field in Properties.

The image is already in .ico format: this is the link to the icon I'm trying to use.

Does anyone know why Microsoft Visual Studio would be displaying this error?

Argument 'picture' must be a picture that can be used as an Icon

Any help would be great.

Upvotes: 23

Views: 38457

Answers (6)

Trần Nam
Trần Nam

Reputation: 13

In my situation. I need to add an Icon to imageList and this error occure.

var icon = new Icon(IconStream)
imageList1.Images.Add(file.Id, icon);

I fixed to this and it's work

var bitmap = new Bitmap(iconStream);
var icon = Icon.FromHandle(bitmap.GetHicon());
imageList1.Images.Add(file.Id, icon);

Hope it help.

Upvotes: 0

yoel halb
yoel halb

Reputation: 12711

In my situation the error was because I used a stream and didn't ensure that the stream pointer is at the beginning.

Adding the following line before new Icon(stream) solved the problem:

 stream.Seek(0, SeekOrigin.Begin);

Upvotes: 0

Stefan Đorđević
Stefan Đorđević

Reputation: 595

Credits to Xiaohuan ZHOU for the answer in this question. This function losslessly converts PNG (including transparency) to .ICO file format.

public void ConvertToIco(Image img, string file, int size)
{
    Icon icon;
    using (var msImg = new MemoryStream())
    using (var msIco = new MemoryStream())
    {
        img.Save(msImg, ImageFormat.Png);
        using (var bw = new BinaryWriter(msIco))
        {
            bw.Write((short)0);           //0-1 reserved
            bw.Write((short)1);           //2-3 image type, 1 = icon, 2 = cursor
            bw.Write((short)1);           //4-5 number of images
            bw.Write((byte)size);         //6 image width
            bw.Write((byte)size);         //7 image height
            bw.Write((byte)0);            //8 number of colors
            bw.Write((byte)0);            //9 reserved
            bw.Write((short)0);           //10-11 color planes
            bw.Write((short)32);          //12-13 bits per pixel
            bw.Write((int)msImg.Length);  //14-17 size of image data
            bw.Write(22);                 //18-21 offset of image data
            bw.Write(msImg.ToArray());    // write image data
            bw.Flush();
            bw.Seek(0, SeekOrigin.Begin);
            icon = new Icon(msIco);
        }
    }
    using (var fs = new FileStream(file, FileMode.Create, FileAccess.Write))
    {
        icon.Save(fs);
    }
}

Upvotes: 1

Earth Engine
Earth Engine

Reputation: 10476

We have an application that works fine on 99% of our computers, but in one laptop it pops out this error.

It looks like our issue is that the laptop user set the screen text/image size to 150%. This could cause otherwise working images no longer working. We will see whether this works.

UPDATE

A commenter seems to have the same problem. And yes, we resolved this problem by setting the screen text size to less than 150%.

Upvotes: 7

Daniel Flannery
Daniel Flannery

Reputation: 1178

After a second restart and then opening and re-saving the .ico myself in Gimp, then I was able to import it without any errors. Not too sure what caused this problem but it was just a freak error.

Upvotes: 5

Wingman4l7
Wingman4l7

Reputation: 678

I had this error recently. Some recommendations:

  • make sure the icon is square (16x16, 32x32)
  • try saving it to a PNG and using this free service for conversion : http://www.convertico.com/

Upvotes: 29

Related Questions