我怎么能提示用户选择保存文件的位置? [英] How can I prompt a user to choose a location to save a file?

查看:116
本文介绍了我怎么能提示用户选择保存文件的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的主要形式我有一个名为SavePDFDocument()方法:

In my main Form I have a method called SavePDFDocument():

private void SavePDFDocument()
{
    PDFWrapper pdfWrapper = new PDFWrapper();
    pdfWrapper.CreatePDF(horizontalPictureScroller1.GetPictures(), "pdfDocument.pdf");
}



正如你所看到的,现在我手动在键入姓名文件。我想问用户选择保存位置和什么样的名字给它

As you can see, right now I'm manually typing in a name for the file. I'd like to ask the user to choose where to save it and what name to give it.

这是我使用上述CreatePDF()方法:

This is the CreatePDF() method I'm using above:

public void CreatePDF(List<System.Drawing.Image> images, string filename)
{
    if (images.Count >= 1)
    {
        Document document = new Document(PageSize.LETTER);
        try
        {

            // step 2:
            // we create a writer that listens to the document
            // and directs a PDF-stream to a file

            PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));

            // step 3: we open the document
            document.Open();

            foreach (var image in images)
            {
                iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);

                if (pic.Height > pic.Width)
                {
                    //Maximum height is 800 pixels.
                    float percentage = 0.0f;
                    percentage = 700 / pic.Height;
                    pic.ScalePercent(percentage * 100);
                }
                else
                {
                    //Maximum width is 600 pixels.
                    float percentage = 0.0f;
                    percentage = 540 / pic.Width;
                    pic.ScalePercent(percentage * 100);
                }

                pic.Border = iTextSharp.text.Rectangle.BOX;
                pic.BorderColor = iTextSharp.text.BaseColor.BLACK;
                pic.BorderWidth = 3f;
                document.Add(pic);
                document.NewPage();
            }
        }
        catch (DocumentException de)
        {
            Console.Error.WriteLine(de.Message);
        }
        catch (IOException ioe)
        {
            Console.Error.WriteLine(ioe.Message);
        }

        // step 5: we close the document
        document.Close();
    }
}

有什么建议?

推荐答案

你看看的 SaveFileDialog

private void button1_Click(object sender, System.EventArgs e)
{
     Stream myStream ;
     SaveFileDialog saveFileDialog1 = new SaveFileDialog();

     saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
     saveFileDialog1.FilterIndex = 2 ;
     saveFileDialog1.RestoreDirectory = true ;

     if(saveFileDialog1.ShowDialog() == DialogResult.OK)
     {
         if((myStream = saveFileDialog1.OpenFile()) != null)
         {
             // Code to write the stream goes here.
             myStream.Close();
         }
     }
}

这篇关于我怎么能提示用户选择保存文件的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆