Results 1 to 9 of 9

Thread: HOW TO draw lines by CreateEnhMetaFile?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    5,044

    HOW TO draw lines by CreateEnhMetaFile?

    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
    Prj_DrawShape.zipName:  Demo-DrawShape.jpg
Views: 229
Size:  11.6 KB

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    5,044

    Re: HOW TO draw lines by CreateEnhMetaFile?

    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"
    Name:  EMF_483_NOT500.jpg
Views: 201
Size:  13.7 KB
    Attached Files Attached Files
    Last edited by xiaoyao; Feb 7th, 2025 at 12:37 AM.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    5,044

    Re: HOW TO draw lines by CreateEnhMetaFile?

    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.
    Name:  picturebox_emf_bug.jpg
Views: 126
Size:  27.4 KB
    Attached Files Attached Files
    Last edited by xiaoyao; Feb 7th, 2025 at 10:26 PM.

  4. #4
    Lively Member
    Join Date
    Jun 2016
    Posts
    107

    Re: HOW TO draw lines by CreateEnhMetaFile?

    XiaoYao can you programaticaly convert a image like a photo .Bmp to .EMF metafile byte by byte array (without text) ?

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    5,044

    Re: HOW TO draw lines by CreateEnhMetaFile?

    Quote Originally Posted by xman2000 View Post
    XiaoYao can you programaticaly convert a image like a photo .Bmp to .EMF metafile byte by byte array (without text) ?
    ai can do it
    It is also possible that.net has such a class library

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,739

    Re: HOW TO draw lines by CreateEnhMetaFile?

    Quote Originally Posted by xman2000 View Post
    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.

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,739

    Re: HOW TO draw lines by CreateEnhMetaFile?

    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.

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,739

    Re: HOW TO draw lines by CreateEnhMetaFile?

    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.

  9. #9
    Junior Member anycoder's Avatar
    Join Date
    Jan 2025
    Posts
    21

    Re: HOW TO draw lines by CreateEnhMetaFile?

    WHY emf picture size not 500*500,my pc it's 483*483
    According to the documentation the dimensions of emf image should be stored in units of 0.01 mm.

    https://learn.microsoft.com/en-us/wi...teenhmetafilea

    Try this sample:
    Code:
    With uRect
            .Left = 0
            .Top = 0
           .Right = 500 * Screen.TwipsPerPixelX * 1.764
           .Bottom = 500 * Screen.TwipsPerPixelY * 1.764
        End With
     ....
       Ellipse hMetaDC, 0, 0, 500 * 72& / 96&, 500 * 72& / 96&

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width