【问题标题】:copy image to clipboard and let it be pasted as file (vb.net)将图像复制到剪贴板并将其粘贴为文件(vb.net)
【发布时间】:2011-01-03 17:52:09
【问题描述】:

我有一个图片框,如果我使用下面的片段:

Clipboard.SetImage(PictureBox.image)

然后我只能将图像粘贴到Paint和MS word之类的东西中。我无法将其作为文件粘贴到文件夹/桌面中。

那么如何将图像复制到剪贴板,如果粘贴到文件夹中,则它会变成文件?

【问题讨论】:

    标签: vb.net image file clipboard pasting


    【解决方案1】:

    如果您使用 .net 并且您的最终目标是保存文件,那么有很多更简单的方法,

    这里是 C# 中的代码,将其移植到 VB.net 中并不难,我只是懒得这样做 :) 无论如何,您必须先将其保存在某个地方,然后才能粘贴它...

    它将文件加载到图片框中,然后再次将其保存到文件中,(蹩脚,我知道) 并将剪贴板数据设置为复制操作

    然后当您粘贴 (Ctrl+V) 时,它会被粘贴。

    C# __ 位图 bmp; 字符串文件名=@"C:\image.bmp"; //这里我假设你从文件中加载它,你可能从其他地方获取图像,你的代码可能不同 pictureBox1.Image=(Image) Bitmap.FromFile(fileName); bmp=(Bitmap)pictureBox1.Image; bmp.Save(@"c:\image2.bmp"); System.Collections.Specialized.StringCollection st = new System.Collections.Specialized.StringCollection(); st.Add(@"c:\image2.bmp"); System.Windows.Forms.Clipboard.SetFileDropList(st); </pre>

    viola 尝试将文件 image2.bmp 粘贴到一个文件夹中。

    【讨论】:

    • 我知道要保存它们,但是我想将图像复制为文件。
    • 现在无法将图像粘贴到 MSPaint 中。如何解决在MSPaint AND MS Word中使用?
    【解决方案2】:

    这基本上是@Vivek 发布但移植到 VB 的内容。如果这对你有用,请投票给他。您必须了解的是,资源管理器仅允许您粘贴文件,而不是对象(无论如何都是 AFAIK)。原因是如果你将图像数据复制到剪贴板,它应该以什么格式粘贴? PNG,BMP,JPG?什么压缩设置?所以就像@Vivek 说的,你需要考虑一下,在系统的某个地方自己创建一个文件,然后使用SetFileDropList 将临时文件添加到剪贴板。

    '   Add it as an image
        Clipboard.SetImage(PictureBox1.Image)
    
        'Create a JPG on disk and add the location to the clipboard
        Dim TempName As String = "TempName.jpg"
        Dim TempPath As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, TempName)
        Using FS As New System.IO.FileStream(TempPath, IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Read)
            PictureBox1.Image.Save(FS, System.Drawing.Imaging.ImageFormat.Jpeg)
        End Using
        Dim Paths As New System.Collections.Specialized.StringCollection()
        Paths.Add(TempPath)
        Clipboard.SetFileDropList(Paths)
    

    【讨论】:

      猜你喜欢
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      相关资源
      最近更新 更多