English 中文(简体)
如何将图像从镜头中解救到某些途径
原标题:How to save image to selected path from Stream object

下述法典促使用户选择一条从图像Box中挽救形象的道路:

        Stream myStream;
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();            
        saveFileDialog1.Filter = "Portable Network Graphics|*.png";
        saveFileDialog1.Title = "Bild speichern";
        saveFileDialog1.RestoreDirectory = true;
        saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if ((myStream = saveFileDialog1.OpenFile()) != null)
            {
                this.picBox.Image.Save(myStream.ToString()); // is not getting the selected path
                myStream.Close();
            }
        }

But how can I get the path from myStream or save the image to the user defined location (with compatibility to .NET 3.5)?

最佳回答

如果你想从除方言中找到选定的档案途径,则使用......

saveFileDialog1.FileName;

See here, 提供更多有关这一财产的信息。

You don t need to worry about using a Stream for this task.

简言之,这就是你的法典应当是什么......

SaveFileDialog saveFileDialog1 = new SaveFileDialog();            
saveFileDialog1.Filter = "Portable Network Graphics|*.png";
saveFileDialog1.Title = "Bild speichern";
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    this.picBox.Image.Save(saveFileDialog1.FileName);
}
问题回答

you can work with the SaveFileDialog.FileName only, no need for separated streams, try this:

using (var saveFileDialog1 = new SaveFileDialog())
{
    saveFileDialog1.Filter = "Portable Network Graphics|*.png";
    saveFileDialog1.Title = "Bild speichern";
    saveFileDialog1.RestoreDirectory = true;
    saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        picBox.Image.Save(saveFileDialog1.FileName);
    }
}

You can use:

string path = Path.GetDirectory(saveFileDialog1.Filename);
this.picBox.Image.Save(saveFileDialog1.Filename);

确实,你不需要一流去做:

How s this?

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Portable Network Graphics|*.png";
saveFileDialog1.Title = "Bild speichern";
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    (using FileStream fStr = new FileStream(saveFileDialog1.FileName, FileMode.Create))
    {
        this.picBox.Image.Save(fStr);
        fStr.Close();
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签