1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
Imports System
Imports System.Windows.Forms
Imports System.Text.ASCIIEncoding
Imports System.Drawing.Imaging
Imports System.IO
Public Class frmEmf1
Private pathApp As String = Directory.GetCurrentDirectory & "\"
'Nom du fichier de sauvegarde
Private FileName1 As String = "test.emf"
Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
End Sub
'cree un metafile et le sauvegarde
Private Sub btnDrawSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDrawSave.Click
Dim tempGr As Graphics = Me.PictureBox1.CreateGraphics()
Dim intPtrHdc As IntPtr = tempGr.GetHdc()
'1/cree un fichier sur disque
Dim myMetaFile As Metafile = New Metafile(pathApp & FileName1, intPtrHdc)
'2/ objet graphics a partir du metafile
Dim emfGr As Graphics = Graphics.FromImage(myMetaFile)
'3/dessine dessus
emfGr.FillEllipse(Brushes.Yellow, 0, 0, 100, 100)
emfGr.DrawEllipse(Pens.Black, 0, 0, 100, 100)
emfGr.DrawArc(New Pen(Color.Red, 10), 20, 20, 60, 60, 30, 120)
'' Cree des commentaires à ajouter-eventuellement dans
''le fichier.
'Dim encoderASC As New System.Text.ASCIIEncoding()
'Dim metaComment As Byte() = {CByte(AscW("T")), CByte(AscW("e")), CByte(AscW("s")), CByte(AscW("t"))}
'emfGr.AddMetafileComment(metaComment)
' Dispose de emfGr
emfGr.Dispose()
' Release handle & dispose du tempGr.
tempGr.ReleaseHdc(intPtrHdc)
tempGr.Dispose()
'
'4/ Dispose de myMetaFile puisque il est sur disque
myMetaFile.Dispose()
End Sub
'Charge le metafile cree ci-dessus et l'affiche dans un picturebox
Private metaFileToDisplay As String = String.Empty
Private Sub btnLoadDrawMetafile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadDrawMetafile.Click
Dim dlg As OpenFileDialog = New OpenFileDialog
dlg.CheckFileExists = True
dlg.Filter = "fichiers(*.emf)|*.emf"
If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
metaFileToDisplay = dlg.FileName
If Len(FileName1) = 0 Then
MessageBox.Show("select file please...")
Return
Else
'demande à picturebox de se rafraichir.......
Me.PictureBox1.Invalidate()
End If
End If
End Sub
'event paint de rafraichaissement du picturebox
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If metaFileToDisplay Is String.Empty Then Return
Using mf As Metafile = New Metafile(metaFileToDisplay)
e.Graphics.DrawImage(mf, New Point(0, 0))
End Using
End Sub
End Class |
Partager