How to use API to create vector graphics, rectangles, triangles, polygons, fill colors, strokes, and draw lines? Draw vector text?
it's by gdi32 pix pictures:
Code:
Friend Function: Drawpolygons (hdc As Long, e() As POINT API, BorderColor As Long, Optional BorderWidth As Long=2)_
, Optional FillColor As Long) As Long
Dim Graph As Long
Graph = CreatePolygonRgn(E(0), UBound(E) + 1, ALTERNATE)
'LBound(uPoints)), UBound(uPoints) - LBound(uPoints) + 1
Dim hBrush As Long
If FillColor <> 0 Then
hBrush = CreateSolidBrush(FillColor)
Call FillRgn(hdc, Graph, hBrush)
End If
Create a red brush with a width of 3 pixels
Dim hPen As Long
hPen = CreatePen(PS_SOLID, BorderWidth, BorderColor)
Select brush to device context
Dim hOldPen As Long
hOldPen = SelectObject(hdc, hPen)
Draw the border of a polygon
Polyline hdc, E(0), UBound(E) + 1
Restore old paintbrushes
SelectObject hdc, hOldPen
Delete the created brush
DeleteObject hPen
Drawingpolygons=Graph
End Function
WHY emf picture size not 500*500,my pc it's 483*483
Code:
Private Declare Function CreateEnhMetaFile Lib "gdi32" Alias "CreateEnhMetaFileA" (ByVal Hdc As Long, ByVal lpszFileName As String, lpRect As rect, ByVal dwSignature As Long) As Long
Private Declare Function SetMapMode Lib "gdi32" (ByVal hCD As Long, ByVal nMapMode As Long) As Long
Private Declare Function Ellipse Lib "gdi32" ( _
ByVal Hdc As Long, _
ByVal x1 As Long, _
ByVal y1 As Long, _
ByVal x2 As Long, _
ByVal y2 As Long _
) As Long
Private Declare Function CloseEnhMetaFile Lib "gdi32" (ByVal hemf As Long) As Long
Private Declare Function DeleteEnhMetaFile Lib "gdi32" ( _
ByVal hmf As Long _
) As Long
Code:
Dim uRect As rect
Dim hMetaDC As Long
Dim hMetaFile As Long
With uRect
.Left = 0
.Top = 0
.Right = 500 * Screen.TwipsPerPixelX * GetVideoDPIScale
.Bottom = 500 * Screen.TwipsPerPixelY * GetVideoDPIScale
End With
hMetaDC = CreateEnhMetaFile(0, App.Path & "\testoK.emf", uRect, 0)
SetMapMode hMetaDC, 1 ' MM_TEXT = 1
' ??????????????????' ?????
'Drawing a Shape in the Context of Enhanced Metafile Devices: Drawing a Circle
Dim Hdc As Long
Hdc = Form1.Hdc
Ellipse hMetaDC, 0, 0, 500, 500
' ??????????????
'Turn off the enhanced metafile device context
hMetaFile = CloseEnhMetaFile(hMetaDC)
' Delete Enhanced Metafile Handle
DeleteEnhMetaFile hMetaFile
Set Me.Picture = LoadPicture(App.Path & "\testoK.emf")
MsgBox "SaveOK " & App.Path & "\testoK.emf"
Last edited by xiaoyao; Feb 7th, 2025 at 12:37 AM.
There is a bug in the VB6 picturebox control displaying EMF vector images.
The image has become smaller and the control has been stretched to its original size, resulting in distorted display and loss of some pixels IMAGE control displays normally
The picturebox control is set to borderless, and the right and bottom sides of the rectangular EMF graphic will lose one pixel.
Last edited by xiaoyao; Feb 7th, 2025 at 10:26 PM.
XiaoYao can you programaticaly convert a image like a photo .Bmp to .EMF metafile byte by byte array (without text) ?
xman2000, if you're talking about an EMF file that's a vector graphics file (which is typically the case, but not necessarily always), it's important to recognize that we're talking about very fundamentally different image encoding schemes. Most of the image files we deal with are raster graphics, meaning that they have a very specific width & height in terms of pixels, and that the color of each pixel is specified. (JPG files "cheat" on that a bit with their encoding, but even those are fundamentally a pixel width & height.)
In contrast to this, vector graphics files don't represent pixels at all. Rather, these image files are defined as a set of equations which define 2D "vectors" (and possibly other 2D geometric shapes) and their colors. When an image is defined in this way, there is no pre-defined size or shape associated with it, and that's their largest advantage. They can be scaled to any size without "pixelating" as there are initially no pixels.
Ok, to your question. With just a little thought, it becomes obvious that the task of converting a raster graphics file to a vector graphics file is going to be far from trivial. Other than some attempts to use third-party software to do this a few times in the past, I've never looked deeply into this. AFAIK, the VB6 PictureBox has no ability to do this. The GDI can't do it either. And AI says the GdiPlus has no such ability either.
AI recommends Adobe Illustrator to get this done. But personally, this isn't something I'd ever try to do for any production software. The GdiPlus has some excellent raster graphics image scaling calls, with excellent imaging smoothing algorithms, and that's what I'd be using, just always hanging onto the original image.
If I truly wanted a vector graphics image, I'd create it from scratch. That way, it wouldn't have any "conversion artifacts".
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Also, just to quickly add, if you're wanting a raster graphics image, an EMF is absolutely the wrong way to go. EMF files are notoriously difficult to handle in VB6, having many problems with being "one or two pixels off" when being displayed, causing blurring.
Almost any of the other raster encoding options (BMP, TIF, PNG, TGA, GIF, JPG, J2000, HEIF, etc, etc) are a superior alternative.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Also, just to point out ... clip art files and true type fonts are vector graphics files we deal with everyday.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.