简体   繁体   English

将 bitmap 保存在与原始名称相同的文件夹中

[英]Save a bitmap at same folder as the original with a set name

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).然后我想在与原始文件相同的文件夹中复制转换后的图片,并在文件名中添加额外的文本(例如: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 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() .你有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.然后,你可以在 append 一个字符串到这个名字前扩展。

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.这假定您的文件格式是 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.然后,您可以按照@Corak 的建议使用Path class 构建文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM