2010-01-11 67 views
2

我有一個圖片框,如果我使用Clipboard.SetImage(PictureBox.image)然後,我只能將圖像粘貼到像Paint和MS字樣的東西。我無法將其作爲文件粘貼到文件夾/桌面。複製圖像到剪貼板,並將其粘貼爲文件(vb.net)

那麼,如何將圖像複製到剪貼板,如果粘貼到文件夾,那麼它成爲一個文件?

回答

4

如果您在使用.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

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) 
+0

謝謝,我從來沒有想過格式等:) – 2010-01-11 16:34:43

相關問題