dcsimg
Results 1 to 5 of 5

Thread: GdipSetClipPath and AntiAlias

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    195

    GdipSetClipPath and AntiAlias

    Hello, I try to use GdipSetClipPath but apparently this is not compatible with GdipSetSmoothingMode AntiAlias, my idea is to paint an image with the shape of a path but also be able to use the functions of GdipSetImageAttributesColorMatrix

    In the following example you can better understand my intention.
    Code:
    Option Explicit
    Private Declare Function GdiplusStartup Lib "gdiplus" (ByRef token As Long, ByRef inputbuf As GDIPlusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
    Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hdc As Long, ByRef Graphics As Long) As Long
    Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal Graphics As Long) As Long
    Private Declare Function GdiplusShutdown Lib "gdiplus" (ByVal token As Long) As Long
    Private Declare Function GdipSetSmoothingMode Lib "gdiplus" (ByVal Graphics As Long, ByVal SmoothingMd As Long) As Long
    Private Declare Function GdipCreatePath Lib "GdiPlus.dll" (ByRef mBrushMode As Long, ByRef mpath As Long) As Long
    Private Declare Function GdipDeletePath Lib "GdiPlus.dll" (ByVal mpath As Long) As Long
    Private Declare Function GdipLoadImageFromFile Lib "GdiPlus.dll" (ByVal mFilename As Long, ByRef mImage As Long) As Long
    Private Declare Function GdipDisposeImage Lib "GdiPlus.dll" (ByVal mImage As Long) As Long
    Private Declare Function GdipSetClipPath Lib "GdiPlus.dll" (ByVal mGraphics As Long, ByVal mpath As Long, ByVal mCombineMode As Long) As Long
    Private Declare Function GdipDrawImageRectRectI Lib "gdiplus" (ByVal hGraphics As Long, ByVal hImage As Long, ByVal dstX As Long, ByVal dstY As Long, ByVal dstWidth As Long, ByVal dstHeight As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal srcWidth As Long, ByVal srcHeight As Long, ByVal srcUnit As Long, Optional ByVal imageAttributes As Long = 0, Optional ByVal Callback As Long = 0, Optional ByVal CallbackData As Long = 0) As Long
    Private Declare Function GdipAddPathEllipseI Lib "GdiPlus.dll" (ByVal mpath As Long, ByVal mX As Long, ByVal mY As Long, ByVal mWidth As Long, ByVal mHeight As Long) As Long
    Private Declare Function GdipGetImageDimension Lib "GdiPlus.dll" (ByVal mImage As Long, ByRef mWidth As Single, ByRef mHeight As Single) As Long
    Private Declare Function GdipDisposeImageAttributes Lib "gdiplus" (ByVal imageattr As Long) As Long
    Private Declare Function GdipCreateImageAttributes Lib "gdiplus" (ByRef imageattr As Long) As Long
    Private Declare Function GdipSetImageAttributesColorMatrix Lib "gdiplus" (ByVal imageattr As Long, ByVal ColorAdjust As Long, ByVal EnableFlag As Boolean, ByRef MatrixColor As COLORMATRIX, ByRef MatrixGray As COLORMATRIX, ByVal flags As Long) As Long
    Private Declare Function GdipDrawPath Lib "GdiPlus.dll" (ByVal mGraphics As Long, ByVal mPen As Long, ByVal mpath As Long) As Long
    Private Declare Function GdipCreateTexture Lib "GdiPlus.dll" (ByVal mImage As Long, ByVal mWrapMode As Long, ByRef mTexture As Long) As Long
    Private Declare Function GdipFillPath Lib "GdiPlus.dll" (ByVal mGraphics As Long, ByVal mBrush As Long, ByVal mpath As Long) As Long
    Private Declare Function GdipDeleteBrush Lib "gdiplus" (ByVal Brush As Long) As Long
    
    Private Type GDIPlusStartupInput
        GdiPlusVersion              As Long
        DebugEventCallback          As Long
        SuppressBackgroundThread    As Long
        SuppressExternalCodecs      As Long
    End Type
    
    Private Type COLORMATRIX
        m(0 To 4, 0 To 4)           As Single
    End Type
    
    Private Const SmoothingModeAntiAlias    As Long = 4
    Private Const CombineModeReplace = &H0
    
    
    Private Sub Form_Load()
        Dim tGpInput    As GDIPlusStartupInput
        Dim hToken As Long, hPath As Long, hImage As Long
        Dim hGraphics As Long, hAttributes As Long
        Dim Width As Single, Height As Single
        Dim tMatrixColor    As COLORMATRIX, tMatrixGray    As COLORMATRIX
        Dim sImgPath As String
        
        Me.BackColor = vbBlack
        Me.AutoRedraw = True
        
        tGpInput.GdiPlusVersion = 1
        
        sImgPath = "C:\Users\Windows\Desktop\d5a98b6f39294baa5bd9699887f4922e693242f0v2_128.jpg"
        
        '--------Create------
        Call GdiplusStartup(hToken, tGpInput)
        Call GdipCreateFromHDC(Me.hdc, hGraphics)
        Call GdipCreatePath(&H0, hPath)
        Call GdipLoadImageFromFile(StrPtr(sImgPath), hImage)
        Call GdipCreateImageAttributes(hAttributes)
        '---------------------
        
        With tMatrixColor
            .m(0, 0) = 0.299
            .m(1, 0) = 0.299
            .m(2, 0) = 0.299
            .m(0, 1) = 0.587
            .m(1, 1) = 0.587
            .m(2, 1) = 0.587
            .m(0, 2) = 0.114
            .m(1, 2) = 0.114
            .m(2, 2) = 0.114
            .m(3, 3) = 1
        End With
    
        'Gray Scale
        GdipSetImageAttributesColorMatrix hAttributes, &H0, True, tMatrixColor, tMatrixGray, &H0
    
        GdipGetImageDimension hImage, Width, Height
        GdipSetSmoothingMode hGraphics, SmoothingModeAntiAlias '<-- is ignored in ClipPath  :/
        GdipAddPathEllipseI hPath, 0, 0, Width, Height
        
        '----Option 2-- No antialias
        GdipSetClipPath hGraphics, hPath, CombineModeReplace
        GdipDrawImageRectRectI hGraphics, hImage, 0, 0, Width, Height, 0, 0, Width, Height, &H2, hAttributes, 0, 0
        '-----------
        
        '---Option 2---------So if there are antialias, but as I put grayscale?
        'Dim hBrush As Long
        'GdipCreateTexture hImage, &H4, hBrush
        'GdipFillPath hGraphics, hBrush, hPath
        'GdipDeleteBrush hBrush
        '-----------------
        
        '--------Destroy------------
        Call GdipDisposeImageAttributes(hAttributes)
        Call GdipDisposeImage(hImage)
        Call GdipDeletePath(hPath)
        Call GdipDeleteGraphics(hGraphics)
        Call GdiplusShutdown(hToken)
    End Sub
    any image to try
    https://pm1.narvii.com/6790/d5a98b6f...42f0v2_128.jpg
    leandroascierto.com Visual Basic 6 projects

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    195

    Re: GdipSetClipPath and AntiAlias

    a third option but already using another buffer and combining the first two, maybe the final solution, anyway I want to hear some suggestions.

    Code:
    Private Declare Function GdipCreateBitmapFromScan0 Lib "GdiPlus.dll" (ByVal mWidth As Long, ByVal mHeight As Long, ByVal mStride As Long, ByVal mPixelFormat As Long, ByVal mScan0 As Long, ByRef mBitmap As Long) As Long
    Private Declare Function GdipGetImageGraphicsContext Lib "gdiplus" (ByVal Image As Long, hGraphics As Long) As Long
    Private Const PixelFormat32bppARGB  As Long = &H26200A
    Code:
        Dim hBrush As Long
        Dim hGraphics2 As Long, hImage2 As Long
        
        Call GdipCreateBitmapFromScan0(Width, Height, 0&, PixelFormat32bppARGB, ByVal 0&, hImage2)
        Call GdipGetImageGraphicsContext(hImage2, hGraphics2)
        GdipDrawImageRectRectI hGraphics2, hImage, 0, 0, Width, Height, 0, 0, Width, Height, &H2, hAttributes, 0, 0
        GdipCreateTexture hImage2, &H4, hBrush
        GdipFillPath hGraphics, hBrush, hPath
        GdipDeleteBrush hBrush
        Call GdipDeleteGraphics(hGraphics2)
        Call GdipDisposeImage(hImage2)
    leandroascierto.com Visual Basic 6 projects

  3. #3
    Hyperactive Member
    Join Date
    Aug 2016
    Posts
    281

    Re: GdipSetClipPath and AntiAlias

    Quote Originally Posted by LeandroA View Post
    a third option but already using another buffer and combining the first two, maybe the final solution, anyway I want to hear some suggestions.

    Code:
    Private Declare Function GdipCreateBitmapFromScan0 Lib "GdiPlus.dll" (ByVal mWidth As Long, ByVal mHeight As Long, ByVal mStride As Long, ByVal mPixelFormat As Long, ByVal mScan0 As Long, ByRef mBitmap As Long) As Long
    Private Declare Function GdipGetImageGraphicsContext Lib "gdiplus" (ByVal Image As Long, hGraphics As Long) As Long
    Private Const PixelFormat32bppARGB  As Long = &H26200A
    Code:
        Dim hBrush As Long
        Dim hGraphics2 As Long, hImage2 As Long
        
        Call GdipCreateBitmapFromScan0(Width, Height, 0&, PixelFormat32bppARGB, ByVal 0&, hImage2)
        Call GdipGetImageGraphicsContext(hImage2, hGraphics2)
        GdipDrawImageRectRectI hGraphics2, hImage, 0, 0, Width, Height, 0, 0, Width, Height, &H2, hAttributes, 0, 0
        GdipCreateTexture hImage2, &H4, hBrush
        GdipFillPath hGraphics, hBrush, hPath
        GdipDeleteBrush hBrush
        Call GdipDeleteGraphics(hGraphics2)
        Call GdipDisposeImage(hImage2)

    Code:
    InitGDIPlus
    
    
    GdipCreateBitmapFromFile StrPtr("e:\tx1.jpg"), bitmap '"e:\tx1.jpg",
    GdipGetImageWidth bitmap, bmW
    GdipGetImageHeight bitmap, bmH
    GdipCreateTexture2I bitmap, WrapModeTile, 0, 0, bmW, bmH, brush
    
    GdipCreateBitmapFromScan0 bmW, bmH, 0, PixelFormat32bppARGB, ByVal 0, newImg
    GdipGetImageGraphicsContext newImg, newGraphics
    GdipGraphicsClear newGraphics, &HFFFFFF 
    
    GdipCreateFromHDC Picture2.hDC, Graphics
    GdipSetSmoothingMode Graphics, SmoothingModeAntiAlias
    GdipCreatePath FillModeAlternate, Path
    GdipAddPathArcI Path, 0, 0, bmW, bmH, 0, 360
    GdipFillPath newGraphics, brush, Path
    GdipDrawImageRectI Graphics, newImg, 0, 0, bmW, bmH

  4. #4
    Hyperactive Member
    Join Date
    Aug 2016
    Posts
    281

    Re: GdipSetClipPath and AntiAlias

    Quote Originally Posted by LeandroA View Post
    a third option but already using another buffer and combining the first two, maybe the final solution, anyway I want to hear some suggestions.

    Code:
    Private Declare Function GdipCreateBitmapFromScan0 Lib "GdiPlus.dll" (ByVal mWidth As Long, ByVal mHeight As Long, ByVal mStride As Long, ByVal mPixelFormat As Long, ByVal mScan0 As Long, ByRef mBitmap As Long) As Long
    Private Declare Function GdipGetImageGraphicsContext Lib "gdiplus" (ByVal Image As Long, hGraphics As Long) As Long
    Private Const PixelFormat32bppARGB  As Long = &H26200A
    Code:
        Dim hBrush As Long
        Dim hGraphics2 As Long, hImage2 As Long
        
        Call GdipCreateBitmapFromScan0(Width, Height, 0&, PixelFormat32bppARGB, ByVal 0&, hImage2)
        Call GdipGetImageGraphicsContext(hImage2, hGraphics2)
        GdipDrawImageRectRectI hGraphics2, hImage, 0, 0, Width, Height, 0, 0, Width, Height, &H2, hAttributes, 0, 0
        GdipCreateTexture hImage2, &H4, hBrush
        GdipFillPath hGraphics, hBrush, hPath
        GdipDeleteBrush hBrush
        Call GdipDeleteGraphics(hGraphics2)
        Call GdipDisposeImage(hImage2)

    Code:
    InitGDIPlus
    
    
    GdipCreateBitmapFromFile StrPtr("e:\tx1.jpg"), bitmap '"e:\tx1.jpg",
    GdipGetImageWidth bitmap, bmW
    GdipGetImageHeight bitmap, bmH
    GdipCreateTexture2I bitmap, WrapModeTile, 0, 0, bmW, bmH, brush
    
    GdipCreateBitmapFromScan0 bmW, bmH, 0, PixelFormat32bppARGB, ByVal 0, newImg
    GdipGetImageGraphicsContext newImg, newGraphics
    GdipGraphicsClear newGraphics, &HFFFFFF 
    
    GdipCreateFromHDC Picture2.hDC, Graphics
    GdipSetSmoothingMode Graphics, SmoothingModeAntiAlias
    GdipCreatePath FillModeAlternate, Path
    GdipAddPathArcI Path, 0, 0, bmW, bmH, 0, 360
    GdipFillPath newGraphics, brush, Path
    GdipDrawImageRectI Graphics, newImg, 0, 0, bmW, bmH

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    195

    Re: GdipSetClipPath and AntiAlias

    Hi @xxdoc123, good point, it's 15% faster that way, thank you very much.
    leandroascierto.com Visual Basic 6 projects

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width