简体   繁体   English

C# 使用 SaveFileDialog 或 FolderBrowserDialog 保存图像文件

[英]C# Save Image files using SaveFileDialog or FolderBrowserDialog

I'm trying to save image files which are converted from PDF to PNG.我正在尝试保存从 PDF 转换为 PNG 的图像文件。 I want my application to save the converted image if the PDF was a single page document using the "SaveFileDialog", and if the PDF file was a multi-page document, I then want my application to save them into a folder using the "FolderBrowserDialog".如果 PDF 是使用“SaveFileDialog”的单页文档,我希望我的应用程序保存转换后的图像,如果 PDF 文件是多页文档,我希望我的应用程序使用“FolderBrowserDialog”将它们保存到一个文件夹中”。

My problem is that if the PDF file was a multi-page document, my code would first save the first image (after conversion) using the "SaveFileDialog" before attempting to save the rest of the images using "FolderBrowserDialog".我的问题是,如果 PDF 文件是多页文档,我的代码将首先使用“SaveFileDialog”保存第一张图像(转换后),然后再尝试使用“FolderBrowserDialog”保存图像的 rest。

Here is what I've tried.这是我试过的。

Image = imageToConvert = null;

for (int i = 0; i < images.Length; i++)
{
    if (i == 0)
    {
        //Save converted image if PDF is single page
         imageToConvert = images[i];

        SaveFileDialog _saveFile = new SaveFileDialog();
        _saveFile.Title = "Save file";
        _saveFile.Filter = "PNG|*.png";
        _saveFile.FileName = Lbl_OriginalFileName.Text;


        if (_saveFile.ShowDialog() == DialogResult.OK)
        {
            imageToConvert.Save(_saveFile.FileName, ImageFormat.Png);

            imageToConvert.Dispose();
        }
        else if (_saveFile.ShowDialog() == DialogResult.Cancel)
        {
            return;
        }
    }
    else
    {
        if (i > 0)
        {
            // Save converted Images if PDF is multi-page
            Image imageToConvert2 = images[i];

            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();
            fbd.Description = "Select the folder you want save your files into.";

            string pathString = Path.Combine(fbd.SelectedPath, subFolder);
            Directory.CreateDirectory(pathString);

            if (fbd.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            
                string saveFileNamesPNG = string.Format(Lbl_OriginalFileName.Text + "_" + i.ToString() + ".png", ImageFormat.Png);
                imageToConvert.Save(Path.Combine(pathString, saveFileNamesPNG));
           
            imageToConvert.Dispose();
        }
    }
}

I would really appreciate any help.我真的很感激任何帮助。

I moved the test outside the loop and then checked if it is one page and use the SaveFileDialog.我将测试移到循环外,然后检查它是否是一页并使用 SaveFileDialog。 And if there are more than one, I then used a FolderBrowserDialog with the For-loop to save the images.如果有多个,我会使用带有 For 循环的 FolderBrowserDialog 来保存图像。

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

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