Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a string to an ImageFormat class property?

Tags:

Currently I have this:

printscreen.Save(myOutputLocation + myImageName + myImageExtension, ImageFormat.Png);

However, I would like to specify the ImageFormat class property with a string, but I can't get it to work:

string myString = textBox1.Text;
printscreen.Save(myOutputLocation + myImageName + myImageExtension, ImageFormat.myString);
like image 557
Xrio Avatar asked Aug 01 '17 22:08

Xrio


2 Answers

I would write a method ParseImageFormat using reflection, and use it as

printscreen.Save(myOutputLocation + myImageName + myImageExtension, 
                 ParseImageFormat(myString));

where myString should be one of MemoryBmp,Bmp,Emf,Wmf,Gif,Jpeg,Png,Tiff,Exif,Icon


public static ImageFormat ParseImageFormat(string str)
{
    return (ImageFormat)typeof(ImageFormat)
            .GetProperty(str, BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase)
            .GetValue(null);
}
like image 59
L.B Avatar answered Sep 20 '22 23:09

L.B


You can use only any of defined formats of ImageFormat:

Bmp, Emf, Exif, Gif, Guid, Icon, Jpeg, MemoryBmp, Png, Tiff, Wmf

If you want to set your own format then use

printscreen.Save(myOutputLocation + myImageName + myImageExtension);
like image 43
Roman Marusyk Avatar answered Sep 21 '22 23:09

Roman Marusyk