简体   繁体   English

允许用户从图片框中复制图像并将其保存在任何地方

[英]Allow user to copy image from picturebox and save it everywhere

In my application I have a pictureBox that shows an image. 在我的应用程序中,我有一个显示图像的pictureBox When user right clicks on the pictureBox and selects Copy from the context menu, I want to copy the image into the clipboard so the user can paste it in folders and anywhere else. 当用户右键单击pictureBox并从上下文菜单中选择Copy ,我想将图像复制到剪贴板中,以便用户可以将其粘贴到文件夹和其他任何位置。 How can I do that? 我怎样才能做到这一点?

EDIT: I use this code but by this user only can paste image into word. 编辑:我使用此代码但该用户只能将图像粘贴到word中。

var img = Image.FromFile(pnlContent_Picture_PictureBox.ImageLocation);
Clipboard.SetImage(img);

Clipboard.SetImage copies the image content (binary data) to the clipboard not the file path. Clipboard.SetImage将图像内容(二进制数据)复制到剪贴板而不是文件路径。 To paste a file in Windows Explorer you need to have file paths collection in the clipboard not their content. 要在Windows资源管理器中粘贴文件,您需要在剪贴板中收集文件路径而不是其内容。

You can simply add the path of that image file to a StringCollection and then call the SetFileDropList method of Clipboard to achieve what you want. 您可以简单地将该图像文件的路径添加到StringCollection ,然后调用ClipboardSetFileDropList方法来实现您想要的效果。

System.Collections.Specialized.StringCollection FileCollection = new System.Collections.Specialized.StringCollection();
FileCollection.Add(pnlContent_Picture_PictureBox.ImageLocation);
Clipboard.SetFileDropList(FileCollection);

Now user can past the file anywhere eg Windows Explorer. 现在用户可以在任何地方通过文件,例如Windows资源管

More info on Clipboard.SetFileDropList Method http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.setfiledroplist.aspx 有关Clipboard.SetFileDropList Method更多信息http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.setfiledroplist.aspx

This is the solution when the picture box does not display a file image, but it is rendered upon with GDI+. 当图片框不显示文件图像时,这是解决方案,但是使用GDI +进行渲染。

public partial class Form1 : Form
{
    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        // call render function
        RenderGraphics(e.Graphics, pictureBox1.ClientRectangle);
    }

    private void pictureBox1_Resize(object sender, EventArgs e)
    {
        // refresh drawing on resize
        pictureBox1.Refresh();
    }

    private void copyToClipboardToolStripMenuItem_Click(object sender, EventArgs e)
    {
        // create a memory image with the size taken from the picturebox dimensions
        RectangleF client=new RectangleF(
            0, 0, pictureBox1.Width, pictureBox1.Height);
        Image img=new Bitmap((int)client.Width, (int)client.Height);
        // create a graphics target from image and draw on the image
        Graphics g=Graphics.FromImage(img);
        RenderGraphics(g, client);
        // copy image to clipboard.
        Clipboard.SetImage(img);
    }

    private void RenderGraphics(Graphics g, RectangleF client)
    {
        g.SmoothingMode=SmoothingMode.AntiAlias;
        // draw code goes here
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM