Library tutorials & articles

PictureBox Control

Introduction

The picture box is a fairly simple control that allows you to display, draw and save images. It also can act as a container - it can contain other controls. Both of these uses are explained in this tutorial.

Comments

  1. 26 Jan 2007 at 17:56

    I'm using a picturebox to display text and I would like to know if anyone can please tell me the code to printout the picturebox. The picturebox is called PictureResult.

    Thanks

  2. 05 Dec 2006 at 09:21

    Hi, can anybody please help me, my project is a keypress game, where randomly generated letter are suppose to move down the screen and before they get to the bottom of the screen the player has to press the correct key. is there anyway of animating the text in a picture box so it moves down as the each interval of a timer passes please please help.

  3. 11 Sep 2006 at 22:53

    Hi I wish to save the clipboard from an access  2000 database which has an active x Web Browser built into it.

    Using  the following command  Call keybd_event(vbKeySnapshot, 0, 0, 0)

    I can copy the currennt page
       

    I can successfuly get an image into the clipboard. (this work by doing Control and V into a word/excel doc.)

    However I really want to be able direct to a file.

    I have tried using the following statement

    SavePicture Clipboard.GetData(vbCFBitmap), "c:\screenshot.bmp"

    But get a message Object required. Any ideas to develop further

    Thanks

     

     

     

     

     

     

     

     

     

  4. 11 May 2006 at 08:43

    Hi,

    I want to Copy a Picture from access Database Directly to a PictureBox Control with vb.NET, with this code for line  

    picBLOB.Image = Image.FromStream(stmBLOBData)

    I got error. The message is 'Invalid parameter used.'

    Is it due to Ms Access images storing mechanism?

    Any help is appreciable.

    Thanks,

     

    Vaishali

  5. 09 Jan 2006 at 19:52

    I'm using a picturebox to display text.  This in itself is not a problem.  The problem comes because part of the text remains the same and part is constantly changing.  The part that is changing will keep writing over itself and after just a short while is unreadable because it doesn't remove the old before printing the new data.  I need to find a way to delete a small section of text before I print new text.  I know many would say don't use the picture box but because of many reasons I almost have to.  I'm reading data from an old DOS system that sends out header and label information and the uses X & Y data to locate and print data.  There are many sections that do the same thing and to create labels and such for each section would be a massive mess.


    Any help would be appreciated.


    Jim

  6. 18 Oct 2005 at 11:56

    Create the Visual Basic .NET Sample
    1. Create a SQL Server table or an Access table with the following structure:
    CREATE TABLE BLOBTest
    (
    BLOBID INT IDENTITY NOT NULL,
    BLOBData IMAGE NOT NULL
    )
                       


    2. Follow these steps to create a new Visual Basic Windows Forms application: a.  Start Visual Studio .NET.
    b.  On the File menu, point to New, and then click Project.
    c.  In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Application under Templates.


    3. Drag a PictureBox control and two Button controls from the toolbox to the default form, Form1. Change the Name property of the PictureBox to picBlob. Set the Text property of Button1 to File to Database, and then set the Text property of Button2 to Database to PictureBox.
    4. Add the following Imports statements at the top of the Form1 code module:
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.IO
    Imports System.Drawing.Imaging
                       


    5. Add the following declaration for the database connection string below Public Class Form1, and then modify the connection string as necessary for your environment:
       Dim strCn As String = "Data Source=<server>;" & _
               "Initial Catalog=<database>;Integrated Security=SSPI"
                       


    6. Add the following code in the Click event procedure of Button1, and then modify the file path to an available sample image file as necessary:
           Dim cn As New SqlConnection(strCn)
           Dim cmd As New SqlCommand("INSERT INTO BLOBTest (BLOBData) " & _
               "VALUES (@BLOBData)", cn)
           Dim strBLOBFilePath As String = _
               "C:\Documents and Settings\All Users\Documents" & _
               "\My Pictures\Sample Pictures\winter.jpg"
           Dim fsBLOBFile As New FileStream(strBLOBFilePath, _
               FileMode.Open, FileAccess.Read)
           Dim bytBLOBData(fsBLOBFile.Length() - 1) As Byte
           fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length)
           fsBLOBFile.Close()
           Dim prm As New SqlParameter("@BLOBData", SqlDbType.VarBinary, _
               bytBLOBData.Length, ParameterDirection.Input, False, _
               0, 0, Nothing, DataRowVersion.Current, bytBLOBData)
           cmd.Parameters.Add(prm)
           cn.Open()
           cmd.ExecuteNonQuery()
           cn.Close()
                           
    This code uses a FileStream object to read the image file from disk to a Byte array and then uses a parameterized Command object to insert the data in the database.
    7. Add the following code in the Click event procedure of Button2:
           Dim cn As New SqlConnection(strCn)
           Dim cmd As New SqlCommand("SELECT BLOBID, " & _
               "BLOBData FROM BLOBTest ORDER BY BLOBID", cn)
           Dim da As New SqlDataAdapter(cmd)
           Dim ds As New DataSet()
           da.Fill(ds, "BLOBTest")
           Dim c As Integer = ds.Tables("BLOBTest").Rows.Count
           If c > 0 Then
               Dim bytBLOBData() As Byte = _
                   ds.Tables("BLOBTest").Rows(c - 1)("BLOBData")
               Dim stmBLOBData As New MemoryStream(bytBLOBData)
               picBLOB.Image = Image.FromStream(stmBLOBData)
           End If
                           
    This code retrieves the rows from the BLOBTest table in the database into a DataSet object, copies the image that is most recently added into a Byte array and then into a MemoryStream object, and then loads the MemoryStream into the Image property of the PictureBox control.
    8. Run the project.
    9. Click File to Database to load at least one sample image into the database.
    10. Click Database to PictureBox to display the image that you saved in the PictureBox control.
    11. To retrieve the image in a DataReader object instead of a DataSet object, modify the code in the Click event procedure of Button2 as follows:
           Dim cn As New SqlConnection(strCn)
           Dim cmd As New SqlCommand("SELECT BLOBID, " & _
               "BLOBData FROM BLOBTest ORDER BY BLOBID", cn)
           Dim dr As SqlDataReader
           cn.Open()
           dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
           If dr.Read Then
               Dim bytBLOBData(dr.GetBytes(1, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
               dr.GetBytes(1, 0, bytBLOBData, 0, bytBLOBData.Length)
               Dim stmBLOBData As New MemoryStream(bytBLOBData)
               picBLOB.Image = Image.FromStream(stmBLOBData)
           End If
           dr.Close()
                       


    12. To insert the image from the PictureBox control directly into the database, add a third Button control (Button3) to your form. Set the Text property of Button3 to PictureBox to Database, and then add the following code in the Click event procedure of Button3:
           Dim cn As New SqlConnection(strCn)
           Dim cmd As New SqlCommand("INSERT INTO BLOBTest (BLOBData) " & _
               "VALUES (@BLOBData)", cn)
           Dim ms As MemoryStream = New MemoryStream()
           picBLOB.Image.Save(ms, ImageFormat.Jpeg)
           Dim bytBLOBData(ms.Length - 1) As Byte
           ms.Position = 0
           ms.Read(bytBLOBData, 0, ms.Length)
           Dim prm As New SqlParameter("@BLOBData", SqlDbType.VarBinary, _
               bytBLOBData.Length, ParameterDirection.Input, False, _
               0, 0, Nothing, DataRowVersion.Current, bytBLOBData)
           cmd.Parameters.Add(prm)
           cn.Open()
           cmd.ExecuteNonQuery()
           cn.Close()
                           
    This code retrieves the image data from the PictureBox control in a MemoryStream object, copies the MemoryStream into a Byte array, and then uses a parameterized Command object to save the Byte array to the database.
    13. Run the project. Click Database to PictureBox to dis

  7. 30 Aug 2005 at 09:27

    Hi everyone there,
    Can somebody kindly enough to enlighten me how can i make common dialog boxes like "Save as","Print", print text in a picturebox and save it/copy to clipboard?
    My email is josephtan83@yahoo.com. Urgent.
    THanks!!
    Regards, Joseph.

  8. 19 Jul 2005 at 14:58

    I hope better late then never.


    Once you have the PictureBox print text to it.  Then you have to save the new image to the pictureBox before you can do anything else.  Picture1.Picture = Picture1.Image
    Now just save it to a file as BMP (It’s the only format)
    SavePicture Picture1, "c:\test.bmp"
    Or to the Clip board.
    Clipboard.SetData Picture1.Picture, vbCFDIB



    David Hoegen



  9. 04 Jun 2005 at 08:54

    I have added text to a picturebox, I then saved the picture using savepicture picture1.image, "c:\textinpicture.bmp"    but it only saves the picture and not the text. does anyone have a solution please?

  10. 09 Jun 2004 at 02:53

    is any there to help me
    is there any activeX control to save photo/image/picture in vb to db

  11. 09 Jun 2004 at 02:46

    sunitha_bharadwaj@yahoo.com



    is there any activeX control to save a image/ picture/photo into database

  12. 01 Mar 2004 at 15:01

    Is it possible to rotate a picture in VB?

  13. 21 Dec 2003 at 11:20

    can someone help me?  i want to make a picture in line with text.


    i already know how to add a line with text with the print command, but i cant get a small picture in the same line, like the emoticons from msn messenger.

  14. 05 Dec 2003 at 10:35

    Not sure if there is a better solution but my method is..


    (object).Picture = LoadPicture("")



    where (object) will be your image or picture box name


    ppsssttt  Jared  ...  the caps lock key is on the left side of your keyboard

  15. 08 Sep 2003 at 09:37

    hOW CAN i CHANGE AN IMAGE BACK T OA BLANK LIKE ""



    i ALREADY HAVE A PICTURE, BUT IN ORDER TO GET RID OF DUPLICATE  iWOULD LIKE TO TURN THEM BACK INTO "" SO i CAN REUSE THAT BOX FOR DIFFERENT PICS


    hOW MIGHT i DO THIS AS THE MOST LOGICAL WAY DIDN'T  WORK
    uNLESS i MADE A SIMPLE MISTAKE

  16. 08 Sep 2003 at 09:37

    hOW CAN i CHANGE AN IMAGE BACK T OA BLANK LIKE ""



    i ALREADY HAVE A PICTURE, BUT IN ORDER TO GET RID OF DUPLICATE  iWOULD LIKE TO TURN THEM BACK INTO "" SO i CAN REUSE THAT BOX FOR DIFFERENT PICS


    hOW MIGHT i DO THIS AS THE MOST LOGICAL WAY DIDN'T  WORK
    uNLESS i MADE A SIMPLE MISTAKE

  17. 04 Aug 2003 at 09:35

    well, the picturebox doesn't have a move method, only an event
    and i couldn't find a image object either
    either in .net V10 nor in .net V11
    whats my error?

  18. 26 Feb 2003 at 10:21

    hi Nagesh!!


    thanx for ur reply!! i really appreciate and tried ur method but i'm facing one problem. there's no any setfocus command for picturebox control. is there any other methods to setfocus to picturebox after pressing the command button??

  19. 24 Feb 2003 at 23:17

    Just make in any command button the picturebox .setfocus  and then in the keycode event of picture box wirte the code as If KeyCode = vbKeyRight Then Picture1.Move 100, 100
    I hope this  shud solve your problem

  20. 22 Feb 2003 at 12:47

    hi, everyone!!


    i juz wanna to know that is there any way to control the movements of the image or picture box using keyboard control?? coz i need help badly to complete my projects!!


    if anyone got any idea on it, do reply me soon!! thanks!!

  21. 26 Dec 2002 at 18:14

    Here is an example for updating an Access Database with a jpg file or reading an image from an Access Database and displaying it in a picturebox using the ADODB Stream Object:


    ' Project references the Microsoft Active Data Objects Library v 2.5


    Dim Conn As ADODB.Connection
    Dim RS As ADODB.Recordset


    Const SQLSERVERDATABASE = "Provider=SQLOLEDB;Data Source=MYSERVER; Initial Catalog=MYDATABASE; User ID=MYUSERID;Password=MYPASSWORD;"
    Const ACCESS_DATABASE = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=C:\Code\RPMShell\data\nwind.mdb"


    Private Sub cmdUpdateImage_Click()
    ' You can use code like this to save out to a file, but only bmp supported
    ' SavePicture Picture1.Picture, "c:\winnt\compaq.bmp"


    ' Open your recordset
    Set RS = New ADODB.Recordset
    With RS
    .Source = "Select * From Employees"
    .ActiveConnection = Conn
    .CursorType = adOpenDynamic
    .LockType = adLockOptimistic
    .Open


    ' you can use code like this to add a new record
    '  .AddNew
    '  RS("caption").Value = "compaq logo image"
    '  AddImage RS, "c:\winnt\compaq.bmp"


    ' either add a new record or find an existing one
    .Find "[LASTNAME] = 'Dougan'"
    ' call the function to update the Photo field of the recordset
    AddImage RS, "c:\Code\moitessier.jpg"
    ' close the recordset
    .Close
    End With
    Set RS = Nothing
    MsgBox "Update Complete"
    End Sub


    Private Sub cmdReadImage_Click()


    ' Open your recordset
    Set RS = New ADODB.Recordset
    With RS
    .Source = "Select * From Employees"
    .ActiveConnection = Conn
    .CursorType = adOpenDynamic
    .LockType = adLockOptimistic
    .Open
    ' Find a particular record
    .Find "[LASTNAME] = 'Dougan'"
    ' Save the Photo to a local file
    ReadImage RS, "c:\test.jpg"
    ' Load the local file into a picturebox
    Picture1.Picture = LoadPicture("c:\test.jpg")
    .Close
    End With
    Set RS = Nothing
    MsgBox "Read Complete"
    End Sub


    Sub AddImage(RS As ADODB.Recordset, ByVal FileName As String)
    ' Declare a stream object
    Dim oStream As ADODB.Stream


    ' Create a new instance
    Set oStream = New ADODB.Stream
    oStream.Type = adTypeBinary
    oStream.Open


    ' Tell it the source of the stream
    oStream.LoadFromFile FileName


    'Load the binary object into the field value
    RS.Fields("Photo").Value = oStream.Read


    ' Update the recordset (either here or you could do this elsewhere)
    RS.Update


    ' Close the stream and clean house
    oStream.Close
    Set oStream = Nothing


    End Sub


    Sub ReadImage(RS As ADODB.Recordset, ByVal FileName As String)
    ' Declare a stream object
    Dim oStream As ADODB.Stream


    ' Create a new instance
    Set oStream = New ADODB.Stream
    oStream.Type = adTypeBinary
    oStream.Open


    ' Read the field value into the stream object
    oStream.Write RS.Fields("Photo").Value


    ' Save out to a local file
    oStream.SaveToFile FileName, adSaveCreateOverWrite


    ' Close the stream and clean house
    oStream.Close
    Set oStream = Nothing


    End Sub



    Private Sub FormLoad()
    ' Open your database connection
    Set Conn = New ADODB.Connection
    With Conn
    '    .ConnectionString = RPM
    DATABASE
      .ConnectionString = ACCESS_DATABASE
      .CursorLocation = adUseClient
      .Open
    End With


    End Sub


    Private Sub Form_Unload(Cancel As Integer)
    'Close your database connection
    Conn.Close
    Set Conn = Nothing


    End Sub



    It's not bad if you are not going to have thousands of images, however, if you are, then storing simply the path to the image (You can use UNC names like "\myserver\photos\photo1.jpg" to make it possible to use the same paths from all computers on the network) and then loading the image into a picturebox using LoadPicture as indicated in the above example.

  22. 23 Dec 2002 at 21:43

    .... and dont forget those nasty patents for GIF.. see my signature footer...

  23. 23 Dec 2002 at 17:27

    This is a very cool dll. If you have not already downloaded it, do so by clicking the download link above. On your Windows PC click start + run and type:
    REGSVR32 GIF89.DLL
    This will register the GUID with your system.
    Now open your Visual Basic IDE and click Project + components and select Gif89 1.0. You should now see the control in your tool box. Dbl Click to place it on your form. In the properties look for 'FileName' and enter a path to file example:C:\Graphics\YourAnimatedGraphic.gif
    Now run VB....Cool!!!
    Gif89a is the Compuserve format for animated gifs.


    Charcoal

  24. 23 Dec 2002 at 13:58

    I need to be able save changes to a picturebox control to a image file with the changes / drawing in the new file. I bet this is easy, but I can't seen to be able to do it...


    Please help...

  25. 18 Nov 2002 at 06:54

    Hi, now I am doing some work on the picturebox,one problem is when I maximize the form,how the picture change the size at the same time?

  26. 29 Aug 2002 at 04:07

    hi i am erdem
    i am working on a database project
    everything is ok.,
    adding,updating,deleting,loading..etc.
    but saving and loading an image not ok.
    if you solved your problem then please write me how.
    my e-mail:erdemcengiz82@hotmail.com

  27. 29 Aug 2002 at 04:03

    how can i save an image on to sql server 7.0
    and how can i load it?
    please help me.
    i am searching one week for it.

  28. 29 Jul 2002 at 06:42

    How would you load a picture from a database onto a picture box?

  29. 01 Jan 1999 at 00:00

    This thread is for discussions of PictureBox Control.

Leave a comment

Sign in or Join us (it's free).