I want to make a program that loads a file by input in console, convert the picture into greyscale, negative and blurred. Then I want to make copys of the converted picture in the same folder as the original with extra text at the filename (ex: picture_negative.jpg). My problem is that i can't figure out how i save the pictures in the save folder with the extra text.
This is my first month with programming so im not very experienced.
static void Main(string[] args)
{
Bitmap picture;
try
{
Console.Write("Write the file you want to edit ");
picture = new Bitmap(Console.ReadLine=
}
catch (ArgumentNullException)
{
Console.WriteLine("No picture choosen");
return;
}
catch (ArgumentException)
{
Console.WriteLine("error");
return;
}
ConvertAlgoritms.ConvertAlgoritms.MakePictureGreyScale(picture);
ConvertAlgoritms.ConvertAlgoritms.MakePictureNegative(picture);
ConvertAlgoritms.ConvertAlgoritms.MakePictureBlurred(picture);
}
Example of the class
public class ConvertAlgoritms
{
public static Bitmap MakePictureGreyScale(Bitmap originalPicture)
{
Bitmap newPicture = new Bitmap(originalPicture);
int x, y;
for (x = 0; x < newPicture.Width; x++)
{
for (y = 0; y < newPicture.Height; y++)
{
Color pixel = newPicture.GetPixel(x, y);
int r = pixel.R;
int g = pixel.G;
int b = pixel.B;
int greyScale = (r + g + b) / 3;
newPicture.SetPixel(x, y, Color.FromArgb(greyScale, greyScale, greyScale));
}
}
return newPicture;
}
}
You have Bitmap.Save()
.
First, you must save the entered file name in the console into a variable.
Then, you can append a string to this name before the extension.
Example:
Console.Write("Write the file you want to edit ");
Filename = Console.ReadLine();
picture = new Bitmap(Filename);
string NewFilename = Path.GetFileNameWithoutExtension(Filename)
+ StrToAppend
+ Path.GetExtension(Filename);
picture.Save(Newfilename, picture.RawFormat);
You need to pick up the returned image eg:
var greyscalePicture = ConvertAlgoritms.ConvertAlgoritms.MakePictureGreyScale(picture);
Then you can just save it:
greyscalePicture.Save("Fancynewfilename.jpg", ImageFormat.Jpeg);
This assumes that your file format is JPEG. To generate the new filename you need to keep a reference to the old one.
var originalFilename = Console.ReadLine();
var picture = new Bitmap(originalFilename);
Then you can build the filenames using the Path
class as @Corak suggests.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.