Click here to Skip to main content
15,887,241 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
c# I would like to be able to save images in different formats, but I'm not able to do it.

This doesn't work:


public void CutFromScreen(string cut_from, int x, int y, int w, int h, string save_to)
{

string test = "Png";

//Skær ud efter mus
Bitmap image = new Bitmap(cut_from);
Bitmap image2 = image.Clone(new System.Drawing.Rectangle(x, y, w, h), image.PixelFormat);
image2.Save(save_to, ImageFormat.+test);
image.Dispose();
image2.Dispose();
}
Posted
Comments
Sergey Alexandrovich Kryukov 26-Mar-15 15:15pm    
What are you talking about?
" ImageFormat.+test)" won't even compile.
—SA

1 solution

It won't even compile and makes no sense at all. Can be
C#
image2.Save(save_to, ImageFormat.Png);


https://msdn.microsoft.com/en-us/library/system.drawing.image%28v=vs.110%29.aspx,
https://msdn.microsoft.com/en-us/library/9t4syfhh%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.drawing.imaging.imageformat%28v=vs.110%29.aspx,
https://msdn.microsoft.com/en-us/library/system.drawing.imaging.imageformat.png(v=vs.110).aspx.

Note that ImageFormat.Png itself is not a string (even if you used string syntax correctly), it is also of the type ImageFormat (static read-only property).

Unfortunately, your question clearly shows that you have no clue on how programming works in general, what is source code, compilation, types and instances, properties, all basics like that. I mean, not yet. You can learn all that. Better learn it all before getting to imagine or anything else as advanced or more advanced.

[EDIT]

Variable format? Nothing prevents you from doing it variable. In your code sample, you use the string text, test which is only formally variable, but in fact uses immediate constant in initialization. It makes no difference. You could have a method argument of the type ImageFormat and it will make the method's application of the format variable. What you really pass to it, depends on what comes on input. Usually, this is a user's choice, say, via some list box with choice. And there is no preference in storing strings in it. For understanding, please see my past answers:
combobox.selectedvalue shows {} in winform,
How to copy all the items between listboxes in two forms,
Updating listviews on different windows,
wpf modeless dialog and windows, how to communicate,
Sorted listbox with fractional numbers.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900