C# Argument 'picture' must be a picture that can be used as an Icon
Solution 1
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.
Solution 2
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/
Solution 3
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%.
Solution 4
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);
}
}
Comments
-
Daniel Flannery over 3 years
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 inProperties
.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?
Any help would be great.
-
Charanraj Golla over 10 yearsI used convertico to convert a png to ico but was still getting this error. Tried with a different icon and it worked.
-
mbomb007 about 8 yearsNo restart required for me. Simply open then save with Gimp.
-
donutguy640 over 6 yearsrestart had no effect here either, but saving in Gimp did it.
-
Stefan Đorđević over 3 yearsThe 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.
-
Tim about 2 yearsThe 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 about 2 yearsmsImg can also just be a byte array of a 32x32 pixel PNG and this still works.