23

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.

Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
Daniel Flannery
  • 1,166
  • 8
  • 23
  • 51
  • i just tried it on vs2010 and changed my form's icon and it works.. – Zaki Apr 26 '12 at 12:23
  • It works perfectly, plz paste a screen shot of your error. I even tried in VS 2010 and its working like it is suppose to work – Habib Apr 26 '12 at 12:30
  • Here is the error. I would imagine that it should work fine so this has kinda stumped me. http://i.imgur.com/ueKwJ.jpg – Daniel Flannery Apr 26 '12 at 12:34
  • The icon must be already in a size that is supported. Make a new icon manually in VS to see all the sizes that are supported. What size is your icon? – SimpleVar Apr 26 '12 at 12:43
  • The icon I was trying to import was 32 x 32 which is the same size as the default Icon. I have tried making one myself to check supported sizes and 32 x 32 is not a problem. – Daniel Flannery Apr 26 '12 at 12:53
  • Close VS2010 and open; try again. If that fails, restart your box and try again. – zimdanen Apr 26 '12 at 13:51
  • I tried closing and restarting too there but to no avail. This is a bit of a weird one. – Daniel Flannery Apr 26 '12 at 15:50

5 Answers5

26

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/
Wingman4l7
  • 649
  • 1
  • 8
  • 23
  • I used convertico to convert a png to ico but was still getting this error. Tried with a different icon and it worked. – Charanraj Golla Oct 06 '13 at 10:52
  • 1
    The error occurs if the .ICO file is corrupted, which may happen if, for example, apple.png is renamed to apple.ico. Therefore, it's not a valid .ICO file. Converters like the one you mentioned solve the problem because they don't change file format but convert it instead. – Stefan Đorđević Sep 28 '20 at 00:25
7

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%.

Earth Engine
  • 10,048
  • 5
  • 48
  • 78
5

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.

Richard Ev
  • 52,939
  • 59
  • 191
  • 278
Daniel Flannery
  • 1,166
  • 8
  • 23
  • 51
1

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);
    }
}
Stefan Đorđević
  • 565
  • 1
  • 4
  • 22
  • The code from the creation of icon and later can simply be replaced with `File.WriteAllBytes(file, msIco.ToArray());`. I basically did that, and was lazy and just dumped this into some code I was already working on. Note: I also instantiated *bw* slightly differently: `using (var bw = new BinaryWriter(msIco, Encoding.ASCII, true))`, because I was getting nulls elsewhere, but that may be specific to my code I spliced this into: the *true* at the end keeps the stream open when the binary writer disposes. Either way, this is helpful code, good reference. – Tim Feb 23 '22 at 23:57
  • *msImg* can also just be a byte array of a 32x32 pixel PNG and this still works. – Tim Feb 24 '22 at 00:00
0

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);
yoel halb
  • 12,188
  • 3
  • 57
  • 52