复制图像到剪贴板,并将其粘贴为文件(vb.net)

复制图像到剪贴板,并将其粘贴为文件(vb.net)

问题描述:

我有一个图片框,如果我使用Clipboard.SetImage(PictureBox.image)然后,我只能将图像粘贴到像Paint和MS字样的东西。我无法将其作为文件粘贴到文件夹/桌面。复制图像到剪贴板,并将其粘贴为文件(vb.net)

那么,如何将图像复制到剪贴板,如果粘贴到文件夹,那么它成为一个文件?

如果您在使用.NET和你的最终目标是要保存文件,那里有很多简单的方法,

这里用C代码犀利,将它移植到VB.nt不会很难,我只是懒得做:) 无论如何,你必须保存它在一些地方,然后才能粘贴它...

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

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

 
C# 

___ 

Bitmap bmp; 
string [email protected]"C:\image.bmp"; 
//here I assume you load it from a file , you might get the image from somewere else, your code may differ 

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

and viola尝试粘贴文件夹中的文件image2.bmp将被粘贴。

+0

我知道关于保存它们,但我想复制图像作为文件。 – 2010-01-11 12:48:14

+0

现在不可能将图像粘贴到MSPaint中。如何解决在MSPaint **和** MS Word中使用? – Nasenbaer 2012-12-14 10:20:10

这里基本上是@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) 
+0

谢谢,我从来没有想过格式等:) – 2010-01-11 16:34:43