拍照自动保存相片和手动选择保存相片

1、自动保存相片:指定路径保存C盘保存:

 String savePath = @"C:\";

string filename = $"{DateTime.Now:yyyyMMddHHmmss}.jpg";
 string filepath = savePath + filename;

// 拍照并保存图像

Bitmap image = TakePhoto();

image.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);

2、自动保存相片:当前运行目录下:

    string filename = $"{DateTime.Now:yyyyMMddHHmmss}.jpg";
     string filepath = Path.Combine(Environment.CurrentDirectory, filename);

// 拍照并保存图像

Bitmap image = TakePhoto();

image.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);

3、自动保存相片:桌面:

string filepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), $"{DateTime.Now:yyyyMMddHHmmss}.jpg");

// 拍照并保存图像

Bitmap image = TakePhoto();

image.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);

4、手动保存相片:选择路径保存

saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".jpg";

   // 已选择的文件路径
 string filepath= saveFileDialog1.FileName.ToString();

// 拍照并保存图像

Bitmap image = TakePhoto();

image.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);

5、自动保存相片:当前程序运行新增目录temp下

       string tmpdir = System.IO.Path.Combine(Application.StartupPath, "temp");
                    if (!System.IO.Directory.Exists(tmpdir))
                    {
                       System.IO.Directory.CreateDirectory(tmpdir);
                    }

        
                    string filename = $"{DateTime.Now:yyyyMMddHHmmss}.jpg";
                    string filepath = System.IO.Path.Combine( tmpdir ,filename);

// 拍照并保存图像

Bitmap image = TakePhoto();

image.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);

猜你喜欢

转载自blog.csdn.net/chentiebo/article/details/131138678