如何从图片框中复制图像 [英] How do I copy the image from picture box

查看:151
本文介绍了如何从图片框中复制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试从winforms中的picturebox复制图像.

现在,我已经给出了硬编码的路径和图像名称.
SaveImage(@"C:\ test \ Logo \ Resources \ Logos \ mslogo.png","microsoftlogo.png");

而不是提供硬编码的路径和图像名称,

有没有办法提供文件路径(鼠标右键单击路径和相同的图像名称)

私有void form_MouseClick(对象发送者,MouseEventArgs e)
{
如果(e.Button == System.Windows.Forms.MouseButtons.Right)
{
string [] strButtons = {保存"};

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

私人void frmRightClick_Disposed(object sender,EventArgs e)
{
试试
{
formRightClick frmRightClick =(formRightClick)sender;
字符串strClicked = frmRightClick.strClick.ToUpper

//在这里,我给出了文件路径和图像名称

SaveImage((@@ C:\ test \ Logo \ Resources \ Logos \ mslogo.png","microsoft.png");
}
catch(异常异常)
{
引发异常;
}

}

私有静态无效SaveImage(字符串imagePath,字符串saveName)
{
试试
{
图片originalImage = Image.FromFile(imagePath);
字符串filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),"Downloads",SavedName);
originalImage.Save(filePath,System.Drawing.Imaging.ImageFormat.Png);
}
catch(异常e)
{
抛出e;
}
}

我尝试过的事情:

这是保存图像的方法
我正在调用保存方法SaveImage(@"C:\ test \ Logo \ Resources \ Logos \ mslogo.png","microsoftlogo.png");

私有静态无效SaveImage(字符串imagePath,字符串saveName)
{
试试
{
图片originalImage = Image.FromFile(imagePath);
字符串filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),"Downloads",SavedName);
originalImage.Save(filePath,System.Drawing.Imaging.ImageFormat.Png);
}
catch(异常e)
{
抛出e;
}
}

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;
}
}

推荐答案

您有一个方法可以接受两个字符串:一个是源图像,另一个是您要将新副本另存为的文件名.当前,您正在使用硬编码的字符串来调用它-但是该方法并不关心您传递的内容,只要它们是字符串并且其中一个指向"源图像文件即可.因此,只需将硬编码路径和文件名替换为变量,然后调用相同的方法即可.

我?我会写三种方法:
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:
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);
   }


并使用它们.

注意:原始的try ... catch块无关紧要:根本没有用.


And use them.

Note: Your original try ... catch block is irrelevant: it does nothing useful at all.


//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);
          

        }


    }
 
}


这篇关于如何从图片框中复制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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