Reputation: 195
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);
Upvotes: 8
Views: 3192
Reputation: 116178
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);
}
Upvotes: 6
Reputation: 614
ImageFormat is a quite simple class and all it has is a few properties and properties-like method, so you can't do what you are trying to do. (which is wrong anyway, I'll get to it later.)
You can get the detailed list of the class content here : https://msdn.microsoft.com/en-us/library/system.drawing.imaging.imageformat(v=vs.110).aspx
However, depending on what you are actually trying to do, there might be other way to do this. I'm guessing you are trying to save in a specific format depending on a user input. ImageFormat.mystring
will never work, but if the string is one of the supported formats (see above link), you could probably iterate through the diferent formats to find the seeken one.
A really basic and quite slow way to do this would be sopmething like this. (I haven't done c# in a while, this might be ugly code.)
ImageFormat[] supported_formats = [ImageFormat.GIF, ImageFormat.Bmp]; /* etc */
byte index = 0;
ImageFormat curr_format;
do
{
curr_format = supported_formats[i++];
} while (curr_format.ToString() != user_string);
/* curr_format now holds user-entered format */
Now, as for why what you were trying was wrong.
Unless you do some slighly more complicated stuff, using reflection or decorators (maybe), you can't deal in classes that way. You were trying to force a class (ImageFormat
) to have an attribute (your_string
) which name would change depending on a dynamic input. While it's possible to do so (as probably mentionned in other answers by now), I don't think it's useful for such a simple use case, and you would probably end up eating a lot of time for something (saving) that doesn't look in great need of performances nor seems to be used often anyway.
You can however find documentation on that kind of thing, since it could still be of interest to you, on various website.
Google gave me those : http://www.dofactory.com/net/decorator-design-pattern - Decorators https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection - reflection
Upvotes: 0
Reputation: 24619
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);
Upvotes: 1