如何使用 VB.NET 或 C# 将图元文件(矢量图像)写入剪贴板



我对程序进行编码以创建以wmf作为扩展格式的图元文件。我想复制该文件以粘贴到MS Word和Excel中。

我使用以下代码写入剪贴板:

方式1:

dim st as new system.collections.specialized.stringcollection()

st.add("image path")

=>它可以粘贴在MS Word上,不能粘贴在Excel上。

方式2:

My.Computer.Clipboard.SetImage(picturebox.Image)

=> 不能粘贴到任何地方

方式3:

Using bmp As New System.Drawing.Imaging.Metafile(current_path1)

```    Using pngMs As Stream = New MemoryStream()```
```        bmp.Save(pngMs, Imaging.ImageFormat.Wmf)```
```        data_img.SetData("wmf", pngMs)```
```        Clipboard.SetDataObject(data_img, True)```
```  End Using```
```End Using```

=> 得到错误,如"参数空异常。 参数:编码器">

请告诉我如何将图元文件(wmf 文件(写入剪贴板并将其粘贴到 MS Word 和 Excel 中。 非常感谢。

<DllImport("user32.dll", entrypoint:="OpenClipboard", SetLastError:=True, exactspelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function OpentClipBoard(ByVal hWnd As IntPtr) As Boolean
End Function
<DllImport("user32.dll", EntryPoint:="EmptyClipboard", SetLastError:=True, exactspelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function EmptyClipBoard() As Boolean
End Function
<DllImport("user32.dll", EntryPoint:="SetClipboardData", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function SetClipBoardData(ByVal uFormat As Integer, ByVal hWnd As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="CloseClipboard", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function CloseClipboard() As Boolean
End Function
<DllImport("user32.dll", EntryPoint:="GetClipboardData", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function GetClipboardData(ByVal uFormat As Integer) As IntPtr()
End Function
<DllImport("user32.dll", EntryPoint:="IsClipboardFormatAvailable", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function IsClipboardFormatAvailable(ByVal uFormat As Integer) As Short
End Function
<DllImport("gdi32.dll", EntryPoint:="CopyEnhMetaFileA", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function CopyEnhMetaFile(ByVal hemfSrc As IntPtr, ByVal hNULL As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll", EntryPoint:="DeleteEnhMetaFile", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
Public Shared Function DeleteEnhMetaFile(ByVal hemfSrc As IntPtr) As Boolean
End Function
Public Function func_Put_Metafile_On_Clipboard(ByVal hWnd As IntPtr, ByVal mf As Metafile) As Boolean
Dim bResult As New Boolean()
bResult = False
Dim hEMF, hEMF2 As IntPtr
hEMF = mf.GetHenhmetafile()
If Not hEMF.Equals(New IntPtr(0)) Then
hEMF2 = CopyEnhMetaFile(hEMF, New IntPtr(0))
If Not hEMF2.Equals(New IntPtr(0)) Then
If OpentClipBoard(hWnd) Then
If EmptyClipBoard() Then
Dim hRes As IntPtr
hRes = SetClipBoardData(14, hEMF2)   '14 == CF=ENHMETAFILE
bResult = hRes.Equals(hEMF2)
CloseClipboard()
End If
End If
End If
DeleteEnhMetaFile(hEMF)
End If
Return bResult
End Function

如何使用: 回想一下该功能为func_Put_Metafile_On_Clipboard(IntPtr.Zero,metafile_(

谢谢大家。

试试这个

Clipboard.SetImage(New Bitmap("c:wmf.wmf"))

Clipboard.SetImage(bmp)

最新更新