Results 1 to 2 of 2

Thread: VB6 - GDIPLUS: how draw an image by pixels?

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    VB6 - GDIPLUS: how draw an image by pixels?

    how can i draw an image by pixels?
    i have a function that can do it:
    1 - get the 2 vertical lines points;
    2 - know these points we know the origin and destination horizontal line;
    3 - getting the horizontal line points:
    Code:
    'if the line don't use Z, the Z value must be zero
    Public Function GetLinePoints(ByRef Origin As Position3D, ByRef Destiny As Position3D) As Position3D()
        Dim Steps As Double
        Dim Points() As Position3D
        
        'Get the points line count:
        Steps = Math.Sqr(Math.Abs(Destiny.x - Origin.x) ^ 2 + Math.Abs(Destiny.y - Origin.y) ^ 2 + Math.Abs(Destiny.Z - Origin.Z) ^ 2)
        Steps = Ceil(Steps)
        If (Steps = 0) Then Steps = 1 'void division by zero
        
        'Get the line increment step:
        Dim increment As Position3D
        increment.x = (Destiny.x - Origin.x) / Steps
        increment.y = (Destiny.y - Origin.y) / Steps
        increment.Z = (Destiny.Z - Origin.Z) / Steps
        
        Dim nextpoint As Position3D
        nextpoint = Origin
        Dim i As Integer
        Dim inter As Position3D
        Dim size As Size3D
        ReDim Points(Steps)
        
        'Get all step points:
        For i = 1 To Steps
            nextpoint.x = nextpoint.x + increment.x
            nextpoint.y = nextpoint.y + increment.y
            nextpoint.Z = nextpoint.Z + increment.Z
            inter.x = Math.Round(nextpoint.x)
            inter.y = Math.Round(nextpoint.y)
            inter.Z = Math.Round(nextpoint.Z)
            Points(i).x = inter.x
            Points(i).y = inter.y
            Points(i).Z = inter.Z
            'Debug.Print "X: " + CStr(inter.X) + vbTab + "Y: " + CStr(inter.Y) + vbTab + "Z: " + CStr(inter.Z)
        Next i
        GetLinePoints = Points
    End Function
    4 - now we can have the line points and we just get the image pixel and draw it on that point(in these case we must convert the 3D point to 2D point).
    5 - we draw the pixel...
    Code:
    Friend Sub DrawImageRectanglePoints(DestinationHDC As Long, Points() As Position3D, WorldSize As Size3D)
        'Points(0) is the Upper-Left
        'Points(1) is the Upper-Right
        'Points(2) is the Low-Right
        'Points(3) is the Low-Left
        
        'get graphics from destination HDC:
        Dim memhGraphics As Long
        GdipCreateFromHDC DestinationHDC, memhGraphics
        
        'Get left and right vertical line points:
        Dim PointsUpperDownLeft() As Position3D
        PointsUpperDownLeft = GetLinePoints(Points(0), Points(3))
        Dim PointsUpperDownRight() As Position3D
        PointsUpperDownRight = GetLinePoints(Points(1), Points(2))
        
        Dim x As Long
        Dim y As Long
        Dim R As Long
        Dim G As Long
        Dim B As Long
        
        'Between the left and right vertical line points we get the horizontal line points:
        
        Dim DrawPixelPoints() As Position3D
        For y = 0 To UBound(PointsUpperDownLeft) - 1
            DrawPixelPoints = GetLinePoints(PointsUpperDownLeft(y), PointsUpperDownRight(y))
            For x = 0 To UBound(DrawPixelPoints) - 1
                Dim Point As POINTAPI
                Dim color As Long
                Dim PosX As Long
                Dim PosY As Long
                PosX = x
                PosY = y
                
                'Test the image size for we tiled the image:
                If (PosX >= (ImageWidth - 1)) Then
                    While (PosX >= (ImageWidth - 1))
                        PosX = PosX - ImageWidth
                    Wend
                End If
                
                If (PosY >= (ImageHeight - 1)) Then
                    While (PosY >= (ImageHeight - 1))
                        PosY = PosY - ImageHeight
                    Wend
                End If
                
                'Get the pixel color(ARGB):
                GdipBitmapGetPixel hBitmap, PosX, PosY, color
                
                'Convert ARGB to RGB:
                R = color And &HFF
                G = (color And &HFF00&) \ &H100&
                B = (color And &HFF0000) \ &H10000
                If (color = CLR_INVALID) Then Exit For
                
                'Convert the 3D point to 2D point(from horizontal line):
                Point = ConvertPositon3DTo2D(DrawPixelPoints(x), WorldSize)
                
                'Draw the pixel on point:
                'SetPixelV DestinationHDC, Point.x, Point.y, RGB(R, G, B)
                StretchBlt DestinationHDC, Point.x, Point.y, PointsUpperDownRight(y).x - PointsUpperDownLeft(y).x, 1, hdc, PosX, PosY, PointsUpperDownRight(y).x - PointsUpperDownLeft(y).x, 1, vbSrcCopy
            Next x
        Next y
        GdipDeleteGraphics memhGraphics
    End Sub
    the 'PointsUpperDownRight(y).x - PointsUpperDownLeft(y).x' it's the horizontal line width
    the 'PointsUpperDownRight(y).y - PointsUpperDownLeft(y).y' it's the horizontal line height
    on source i just need 1 image line... that's why the source height it's 1.
    the StretchBlt () function, by some reason, don't draw it
    the SetPixelV () works but, like we know, it's very slow.
    the StretchBlt () it's for draw an image horizontal line directly to DestinationHDC... but by some reason, isn't drawed
    what i'm doing wrong with StretchBlt()?
    maybe i must update the image class for use DIB's for very speed.. but, for now, i need fix these line
    PS: the DrawImageRectanglePoints() it's for rectangle points... but we can use a triangle too... it depends what points we use
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: VB6 - GDIPLUS: how draw an image by pixels?

    heres how i get the hGraphics HDC(when i load an image from a file):
    Code:
    Call GdipCreateFromHDC(hdc, hGraphics)
    VB6 2D Sprite control

    To live is difficult, but we do it.

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