Solved

Store picturebox.image in an imagelist in VB.NET

Posted on 2008-10-03
3
643 Views
Last Modified: 2010-04-21
Hi, experts -
I'm sure I'm just missing something ridiculously simple. I'm drawing a character in a picturebox, using Graphics objects associated with 2 pictureboxes (PB and PB2). After the character is drawn, I'm trying to put the picturebox image into an imagelist control. The imagelist is supposed to take an image of type "System.Drawing.Image" - which is what the picture box image should be (I assume). Yet in the line that is "IList1A.Images.Add (PB.Image)", I get an error saying that "Value can not be null" for PB.Image.

I just got through writing pixels on the bitmap of the PB, I don't see where it should be null. Can someone tell me what I'm doing wrong?

Thanks!

Dim g As Graphics = System.Drawing.Graphics.FromHwnd(PB.Handle)
                        Dim g1 As Graphics = System.Drawing.Graphics.FromHwnd(PB2.Handle)
 
                        For i = 1 To CellWidth
                            For l = Pointer1 + 1 To Pointer1 + 1 + CellHeight
                                'draw the '1's' as black
                                If Mid$(CellString(i), l, 1) = "1" Then
                                    g.DrawEllipse(Pens.Black, i - 1, CellHeight - (l - Pointer1), 1, 1)
                                    g1.DrawEllipse(Pens.Black, i - 1, CellHeight - (l - Pointer1), 1, 1)
                                Else
                                    g.DrawEllipse(Pens.Khaki, i - 1, CellHeight - (l - Pointer1), 1, 1)
                                    g1.DrawEllipse(Pens.DarkKhaki, i - 1, CellHeight - (l - Pointer1), 1, 1)
                                End If
                            Next l
                        Next i
 
                        ICount = ICount + 1
                        If j = 0 Then
                            IList1A.Images.Add(PB.Image)
                            IList1B.Images.Add(PB2.Image)

Open in new window

0
Comment
Question by:LongDave
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22638487
"I just got through writing pixels on the bitmap of the PB, I don't see where it should be null. Can someone tell me what I'm doing wrong?"

No, essentially what you did was draw on TOP of the PictureBox...sorta like drawing in the same layer that would draw its Border if that Style was enabled:

    Dim g As Graphics = System.Drawing.Graphics.FromHwnd(PB.Handle)

If you want to draw on the Image that the PictureBox is DISPLAYING, then you would need:

    Dim g As Graphics = System.Drawing.Graphics.FromImage(PB.Image)

*** BUT *** this assumes that you have actually ASSIGNED a valid Image to the PictureBoxes Image() Property!

By default, a PictureBox doesn't display anything and the Image() Property would return "Nothing"...
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22638505
...what is the end goal here?...the nature of your code makes me wonder if you really need a PictureBox at all!  =\

You probably wanted something more like this:

    Dim bmp As New Bitmap(someWidth, someHeight)
    Dim G As Graphics = Graphics.FromImage(bmp)
    ' ... draw with "G" ...
    G.Dispose()
    IList1A.Images.Add(bmp)
0
 

Author Closing Comment

by:LongDave
ID: 31502937
Idle_Mind .... Not so Idle, m'man. Excellent suggestions - and it appears to work great! Many thanks .. all points are yours.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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