Graphics programming [VB.NET] - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Graphics programming [VB.NET]

wotgivzwotgivz Posts: 12Member
Hi!
I've been told that VB.NET does not support the "pset" and associated graphics functions (commands) that we had in VB6; further, they say that there is no equivalent for the "pset" command in VB.NET!!???
So, do we use the DrawLine function and make the endpoints identical? Isn't that kind of heavy handed?
I like do do graphics programming so I do PSET quite a lot! Anyone have any info about this? Any site refs?
What about BitBlt? Has that changed in VB.NET?
Thanks in advance,
Tej Singh

Regards,
Tej Singh

Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    : Hi!
    : I've been told that VB.NET does not support the "pset" and associated graphics functions (commands) that we had in VB6; further, they say that there is no equivalent for the "pset" command in VB.NET!!???
    : So, do we use the DrawLine function and make the endpoints identical? Isn't that kind of heavy handed?
    : I like do do graphics programming so I do PSET quite a lot! Anyone have any info about this? Any site refs?
    : What about BitBlt? Has that changed in VB.NET?
    : Thanks in advance,
    : Tej Singh
    :
    : Regards,
    : Tej Singh
    :
    :

    Here is equivalent to pset in VB.NET or any .NET language for that matter:

    [code]
    Imports System.Drawing
    ...

    ' Create an image surface that is 100x100 pixels
    Dim bm As New Bitmap(100, 100)

    ' Similar to Pset
    bm.SetPixel(50, 50, Color.Red)

    bm.Dispose
    [/code]

    You may still declare external API functions, such as BitBlt, but sometimes it may be uneccessary. In fact, a GDI+ function that does what BitBlt does in so many words is the Gaphic objects DrawImage function:

    [code]
    Imports System.Drawing
    ...

    Dim mainBm As New Bitmap(100, 100)
    Dim otherBm As New ("myImage.bmp")

    ' Get the graphics object of the main surface
    Dim mainG As Graphics = Graphics.FromImage(mainBm)

    ' Copy other image onto the main image
    mainG.DrawImage(otherBm, 0, 0)

    mainG.Dispose
    mainBm.Dispose
    otherBm.Dispose
    [/code]

    The beautiful thing about DrawImage, is that it has 30 overloads. Meaning 30 different ways to copy an image onto another image.

    If you really want to tap into graphics programming, you can always download Direct-X 9.0 for managed code.

  • wotgivzwotgivz Posts: 12Member
    Hello iwilld0it!
    I tried the code you posted as-is, the one exemplifying the BitBlt/DrawImage function but there's a problem I don't see the BMP I'm expecting to see!
    I pasted the code into a command_button_click sub, prepared a small (100x100 pixel) BMP, and entered the name and folder of the bmp into the code.
    It compiles OK, runs OK, but there's no display!
    And I also don't follow how to run the first example, the one about the the PSet/SetPixel function. I copied the code as-is to a command_button_click sub. It compiles OK, runs OK, but theres no "point"!
    Also when I saw nothing on screen I forced a form refresh, but no luck (in both cases).
    Regards,
    Tej Singh

    : : Hi!
    : : I've been told that VB.NET does not support the "pset" and associated graphics functions (commands) that we had in VB6; further, they say that there is no equivalent for the "pset" command in VB.NET!!???
    : : So, do we use the DrawLine function and make the endpoints identical? Isn't that kind of heavy handed?
    : : I like do do graphics programming so I do PSET quite a lot! Anyone have any info about this? Any site refs?
    : : What about BitBlt? Has that changed in VB.NET?
    : : Thanks in advance,
    : : Tej Singh
    : :
    : : Regards,
    : : Tej Singh
    : :
    : :
    :
    : Here is equivalent to pset in VB.NET or any .NET language for that matter:
    :
    : [code]
    : Imports System.Drawing
    : ...
    :
    : ' Create an image surface that is 100x100 pixels
    : Dim bm As New Bitmap(100, 100)
    :
    : ' Similar to Pset
    : bm.SetPixel(50, 50, Color.Red)
    :
    : bm.Dispose
    : [/code]
    :
    : You may still declare external API functions, such as BitBlt, but sometimes it may be uneccessary. In fact, a GDI+ function that does what BitBlt does in so many words is the Gaphic objects DrawImage function:
    :
    : [code]
    : Imports System.Drawing
    : ...
    :
    : Dim mainBm As New Bitmap(100, 100)
    : Dim otherBm As New ("myImage.bmp")
    :
    : ' Get the graphics object of the main surface
    : Dim mainG As Graphics = Graphics.FromImage(mainBm)
    :
    : ' Copy other image onto the main image
    : mainG.DrawImage(otherBm, 0, 0)
    :
    : mainG.Dispose
    : mainBm.Dispose
    : otherBm.Dispose
    : [/code]
    :
    : The beautiful thing about DrawImage, is that it has 30 overloads. Meaning 30 different ways to copy an image onto another image.
    :
    : If you really want to tap into graphics programming, you can always download Direct-X 9.0 for managed code.
    :
    :

    Regards,
    Tej Singh


  • iwilld0itiwilld0it Posts: 1,134Member
    Ok I put some sample code into the context of a windows Form:

    [code]
    Private Sub DrawSomething(ByVal g As Graphics)
    Dim bm As New Bitmap(100, 100)
    Dim rn As New Random(Environment.TickCount)

    For i As Integer = 10 To 90
    For j As Integer = 10 To 90
    bm.SetPixel(i, j, Color.FromArgb(rn.Next(255), rn.Next(255), rn.Next(255)))
    Next
    Next

    g.DrawImage(bm, 0, 0)
    g.DrawRectangle(Pens.Red, 10, 10, 80, 80)
    bm.Dispose()
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    DrawSomething(e.Graphics)
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    DrawSomething(Me.CreateGraphics())
    End Sub
    [/code]

    Basically I created a windows form and added a Timer. This code demonstrates two ways to get a Graphics object.

    -- In the Paint event, the Graphics object is supplied as part of the PaintEventArgs argument.

    e.Graphics

    -- If you need a Graphics object in any other control event, such as the Timer Tick event, then you call the Form objects CreateGraphics() function.

    Me.CreateGraphics()

    Notice that I pass the Graphics object to a drawing routine which demonstrates the SetPixel method of the Bitmap object and the DrawImage method of the Graphics object. You will see that this code creates a square filled with a collage of random colored pixels.

    Hope this clarifies things.


Sign In or Register to comment.