VB.net économie d'un métafichier / EMF sous forme de bitmap (Tiff)
Question
Actuellement, j'ai un contrôle tiers qui génère un Metafile. Je peux enregistrer le fichier .wmf sur le disque avec en question. Le problème est de savoir comment puis-je rendre le Metafile en tant que fichier Tiff.
À l'heure actuelle, j'ai le code suivant pour obtenir mon métafichier et l'enregistrer.
Dim mf As Metafile = page.GetImage(TXTextControl.Page.PageContent.All)
Dim enhMetafileHandle As IntPtr = mf.GetHenhmetafile()
Dim h As IntPtr
Dim bufferSize As UInteger = GetEnhMetaFileBits(enhMetafileHandle, 0, h)
Dim buffer(CInt(bufferSize)) As Byte
GetEnhMetaFileBits(enhMetafileHandle, bufferSize, buffer)
Dim msMetafileStream As New MemoryStream
msMetafileStream.Write(buffer, 0, CInt(bufferSize))
Dim baMetafileData() As Byte
baMetafileData = msMetafileStream.ToArray
Dim g As Graphics = Graphics.FromImage(mf)
mf.Dispose()
File.WriteAllBytes("c:\a.wmf", baMetafileData)
sub final
_ GetEnhMetaFileBits Fonction publique partagée (ByVal HEMF Comme System.IntPtr, ByVal nTaille Comme UInteger, ByVal comme IntPtr lpData) Comme UInteger End Function
<System.Runtime.InteropServices.DllImportAttribute("gdi32.dll", EntryPoint:="GetEnhMetaFileBits")> _
Public Shared Function GetEnhMetaFileBits(<System.Runtime.InteropServices.InAttribute()> ByVal hEMF As System.IntPtr, ByVal nSize As UInteger, ByVal lpData() As Byte) As UInteger
End Function
J'ai essayé toutes sortes d'image et des appels graphiques et ne peux pas enregistrer le fichier en tant que méta .tiff. Toute suggestion sera appréciée. J'ai même essayé de créer une nouvelle image et d'en tirer le métafichier sur celui-ci. Je finis toujours avec une exception GDI étant levée.
La solution 2
Mon code final ressemble à:
Dim NewGraphic As Graphics = Nothing
Dim BitonalImage As Bitmap = Nothing
Using bmp As New Bitmap(3264, 4224)
Try
NewGraphic = Graphics.FromImage(bmp)
NewGraphic.FillRectangle(New SolidBrush(Color.White), 0, 0, 3264, 4224)
NewGraphic.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
NewGraphic.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
NewGraphic.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
NewGraphic.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
NewGraphic.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
NewGraphic.DrawImage(MetaFileToConvert, 0, 0, 3264, 4224)
BitonalImage = CType(ConvertToBitonal(CType(bmp, Bitmap)), Bitmap)
BitonalImage.SetResolution(385, 385)
OutBMP = BitonalImage
Catch ex As Exception
Throw ex
Finally
NewGraphic.Dispose()
BitonalImage.Dispose()
End Try
End Using
Autres conseils
Votre code est difficile à déchiffrer. dessin simplement le métafichier à un bitmap doit faire le travail. Par exemple:
Dim mf As Metafile = page.GetImage(TXTextControl.Page.PageContent.All)
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("c:\temp\test.tiff", ImageFormat.Tiff)
End Using