Click here to Skip to main content
16,018,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am trying to copy image from picturebox in winforms.

Right now i had given the hard coded path and imagename.
SaveImage(@"C:\test\Logo\Resources\Logos\mslogo.png", "microsoftlogo.png");

Instead of giving the hard coded path and imagename,

Is there a way to give the file path (Mouse Right click path and same Image name)

private void form_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
string[] strButtons ={"Save"};

formRightClick frmRightClick = new formRightClick(strButtons);
frmRightClick.bolPasteToClipBoard = true;
frmRightClick.Show();
frmRightClick.Disposed += new EventHandler(frmRightClick_Disposed);
}
}

private void frmRightClick_Disposed(object sender, EventArgs e)
{
try
{
formRightClick frmRightClick = (formRightClick)sender;
string strClicked = frmRightClick.strClick.ToUpper

// here i am giving the file path and image name

SaveImage((@"C:\test\Logo\Resources\Logos\mslogo.png"", "microsoft.png");
}
catch (Exception exception)
{
throw exception;
}

}

private static void SaveImage(string imagePath, string savedName)
{
try
{
Image originalImage = Image.FromFile(imagePath);
string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", savedName);
originalImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
}
catch (Exception e)
{
throw e;
}
}

What I have tried:

Here is the method to save the image
I am calling save method SaveImage(@"C:\test\Logo\Resources\Logos\mslogo.png", "microsoftlogo.png");

private static void SaveImage(string imagePath, string savedName)
{
try
{
Image originalImage = Image.FromFile(imagePath);
string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", savedName);
originalImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
}
catch (Exception e)
{
throw e;
}
}
Posted
Updated 16-Apr-17 20:37pm

You have a method that accepts two strings: one the source image, the other the filename you want to save the new copy as. Presently, you are calling it with hard coded strings - but the method doesn't care what you pass it, provided that they are strings and that one of them "points at" the source image file. So just replace the hardcode path and filename with variables and call the same method.

Me? I'd write three methods:
C#
private Image LoadImage(string path)
   {
   using (Image im = Image.FromFile(path))
      {
      return new Bitmap(im);
      }
   }

private string GetImageFullPath(string name)
   {
   return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", name);
   }

private void SaveImage(Image im, string destPath)
   {
   im.Save(destPath, System.Drawing.Imaging.ImageFormat.Png);
   }

And use them.

Note: Your original try ... catch block is irrelevant: it does nothing useful at all.
 
Share this answer
 
Comments
Member 12277263 14-Apr-17 14:27pm    
Thanks, but how to call the saveImage method?
OriginalGriff 14-Apr-17 14:37pm    
Image im = LoadImage(pathToImageFile);
SaveImage(im, GetFullImagePath(nameOfFileToSave));
Member 12277263 14-Apr-17 15:10pm    
hiImage im = LoadImage(@"C:\Code\Logo\Resources\Logos\LC0000_.png");
SaveImage(im, GetImageFullPath("Vect.png"));

I am still passing the path from File explorer,
I need the path something like this( "mouse right click + save image " path)
//Please find below solution for your problem, using this you can copy image from picturebox right click copy option and now you can pate that photo to anywhere in you workstation


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            ContextMenu cm = new ContextMenu();
            var item = cm.MenuItems.Add("copy", new EventHandler(abc));
             
            pictureBox1.ContextMenu = cm;
            pictureBox1.ImageLocation = @"C:\\Users\\hp\\Pictures\\Happy-Birthday-Brother-moustache-1.jpg";
        }

        public void abc(object sender, EventArgs e)
        {
           
                System.Collections.Specialized.StringCollection FileCollection = new System.Collections.Specialized.StringCollection();
                FileCollection.Add(pictureBox1.ImageLocation);
                Clipboard.SetFileDropList(FileCollection);
          

        }


    }
 
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900