0

I'm working on my first WPF application, and I'm trying to copy the browsed image into designated folder, however I have problem with CopyTo function.

private void btnBrowse_Click(object sender, EventArgs e)
{
    openFileDialog1.Filter = "Select image(*.JPG;*.PNG)|*.JPG;*.PNG";
    openFileDialog1.Title = "Select your image";
    openFileDialog1.InitialDirectory = @"C:\";

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        // The image to be shown into PictureBox
        pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);

        // The Image
        var theImg = Image.FromFile(openFileDialog1.FileName);

        string fileName = openFileDialog1.FileName;

        if(theImg != null)
        {
            string exePath = System.Environment.GetCommandLineArgs()[0];
            string destinationFolder = Path.Combine(exePath, "Profiles");
            if (!Directory.Exists(destinationFolder))
            {
                Directory.CreateDirectory(destinationFolder);
            }
            string filePath = Path.Combine(destinationFolder, fileName);

            using(var fileStream = new FileStream(filePath, FileMode.Create))
            {
                theImg.CopyTo(theImg, fileStream);
            }
        }
    }
}

Error: No overload for method 'CopyTo' takes 2 arguments

How can I solve this issue? Thank you in advance

  • `Image` [doesn't](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.image?view=dotnet-plat-ext-6.0#methods) have a `CopyTo` method, with two arguments or not. If you want to copy a file the user selected, there is no need to create an image from it. Just [copy](https://stackoverflow.com/a/13012930/11683) it as a file. – GSerg Jun 05 '22 at 11:12
  • You appear to be trying to call `Stream.CopyTo` on an `Image` object. That's obviously not going to work. Maybe do as you should have done in the first place and provide a FULL and CLEAR explanation of the problem. What EXACTLY are you trying to achieve? – user18387401 Jun 05 '22 at 11:15
  • I'm trying to browse some image, and later to save it into folder – Lirka Monteo Jun 05 '22 at 11:17
  • Firstly, note how I said "EXACTLY"? I was asking for DETAILS, not vague generalities. Your problem is specific, so you need to be too. Why do you think you should be able to call `CopyTo` there? – user18387401 Jun 05 '22 at 11:22
  • If you want to save an `Image` object, why would you not call its `Save` method? – user18387401 Jun 05 '22 at 11:22
  • GSerg - I tried with copy but it says 'Image' does not container a definition of 'Copy'.. – Lirka Monteo Jun 05 '22 at 11:25
  • Like @GSerg said, simply use `File.Copy(source, destination);`. Add `using System.IO`. – Paul Sinnema Jun 05 '22 at 11:47

0 Answers0