Reputation: 11
I coding the program to create metafile with wmf as extended format. I want to copy that file to paste at MS Word and Excel.
I using as below code to write to clipboard:
Way 1:
dim st as new system.collections.specialized.stringcollection()
st.add("image path")
=> it can be pasted at MS Word, can not pasted at Excel.
Way 2:
My.Computer.Clipboard.SetImage(picturebox.Image)
=> can not paste anywhere
way 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```
=> get the error such as "ArgumentNullException. parameter:encoder"
Please tell me how to write the metafile (wmf file) to clipboard and paste it at MS Word and Excel. Thank you so much.
Upvotes: 0
Views: 814
Reputation: 1
This code work now on Visual Studio Community 2022
Dim mf As Metafile = Metafile.FromFile(OpenFileDialog1.FileName)
Using bmp As New Bitmap(mf.Width, mf.Height)
Using gr As Graphics = Graphics.FromImage(bmp)
gr.DrawImage(mf, 0, 0)
End Using
bmp.Save(Application.StartupPath & "\tempwmf.bmp", ImageFormat.Bmp)
bmp.Dispose()
End Using
PictureBox1.Image = Image.FromFile(Application.StartupPath & "\tempwmf.bmp")
'result
PictureBox1.Refresh()
Upvotes: 0
Reputation: 576
Try this
Clipboard.SetImage(New Bitmap("c:\wmf.wmf"))
or
Clipboard.SetImage(bmp)
Upvotes: 0
Reputation: 11
<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
how to use: recall that functiion as func_Put_Metafile_On_Clipboard(IntPtr.Zero, metafile_)
Thank all.
Upvotes: 1