pset in

Posted on 2009-12-31
Last Modified: 2013-11-26
Trying to set a pixel in a picturebox with out any luck.
Please look at the code and tell me what is missing.

If I set the 'bmTemp' to the image of the picturebox, why would I want an new instance as the error message suggests ?  The picture box is empty, but does that mean is has no image at all ?

Public Sub DrawAPixel(ByVal x As Integer, ByVal y As Integer, ByVal clr As Color)

        Dim bmTemp As Bitmap

        bmTemp = pctGraph.Image

        Dim MyClr As Color

        bmTemp.SetPixel(x, y, clr)

    End Sub

Open in new window

Question by:sidwelle
    LVL 42

    Accepted Solution

    >>The picture box is empty, but does that mean is has no image at all ?

    yes, that why the error occurs.
    u can't set pixels to an empty bitmap.
    if u want to create a new bitmap, set its pixels and then assign the bitmap to the picture box u need to create the bitmap first with some parameters:

    the code snippet below creates a 50X50 bitmap, set pixels to black and white stripes and then assign to picture box:

    Dim bmTemp As New Bitmap(50, 50)
            For X = 0 To bmTemp.Width - 1
                For Y = 0 To bmTemp.Height - 1
                    bmTemp.SetPixel(X, Y, If(X Mod 2 = 0, Color.Black, Color.White))
                Next Y
            Next X
            pctGraph.Image = bmTemp

    Open in new window

    LVL 38

    Assisted Solution

    The picturebox image property can be assigned a valid image if you wish to display an image.  Until you set it to something it won't have anything.  You can draw on a bitmap, and draw the bitmap on the picturebox like so:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim bmp As Bitmap = New Bitmap(50, 50)
            For i As Integer = 1 To 10
                For j As Integer = 1 To 10
                    bmp.SetPixel(i, j, Color.Red)
            Using g As Graphics = Graphics.FromHwnd(PictureBox1.Handle)
                g.DrawImage(bmp, New Point(0, 0))
            End Using
        End Sub
    End Class

    Open in new window


    Author Closing Comment

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    You could also draw the pixels in the Paint() event of your PictureBox using the supplied "e.Graphics".

    Store the data about which pixels to set in a class Level List type structure.

    Which approach is better really depends on your application...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now