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

6

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.

Drubbing answered 26/4, 2012 at 12:19 Comment(7)
i just tried it on vs2010 and changed my form's icon and it works..Telic
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 workDewberry
Here is the error. I would imagine that it should work fine so this has kinda stumped me. i.imgur.com/ueKwJ.jpgDrubbing
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?Mesa
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.Drubbing
Close VS2010 and open; try again. If that fails, restart your box and try again.Compton
I tried closing and restarting too there but to no avail. This is a bit of a weird one.Drubbing
D
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.

Drubbing answered 30/4, 2012 at 7:49 Comment(2)
No restart required for me. Simply open then save with Gimp.Heirdom
restart had no effect here either, but saving in Gimp did it.Tadd
S
29

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/
Sepia answered 31/5, 2012 at 3:50 Comment(2)
I used convertico to convert a png to ico but was still getting this error. Tried with a different icon and it worked.Gabrielgabriela
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.Ferretti
H
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%.

Hydrophilous answered 15/7, 2014 at 5:23 Comment(0)
D
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.

Drubbing answered 30/4, 2012 at 7:49 Comment(2)
No restart required for me. Simply open then save with Gimp.Heirdom
restart had no effect here either, but saving in Gimp did it.Tadd
A
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);
    }
}
Actualize answered 28/9, 2020 at 0:35 Comment(2)
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.Mciver
msImg can also just be a byte array of a 32x32 pixel PNG and this still works.Mciver
H
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);
Haworth answered 11/5, 2022 at 17:40 Comment(0)
N
0

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.

Nannana answered 1/11, 2023 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.