Getting ImageButton image from database

Discussion in 'Questions (Windows Mobile)' started by glook, Nov 12, 2007.

  1. glook

    glook Member Licensed User

    Simple question, (sorry if this has already been covered - couldn't find it.) - why does a newly loaded image not show, but is OK if passed to another button?

    I have a row of 5 ImageButtons, with no images at the start. They will all shown the same picture: button-1 is loaded from an SQL BLOB and the others are set from the 1st one.

    Button-1 does not show the picture, only a red cross. The other 4 buttons show the image correctly.

    The code is:-
    cmd.CommandText = "SELECT * FROM FileStore WHERE Name = 'camera_pic'"
    reader.Value = cmd.ExecuteReader
    reader.ReadNextRow
    btnPic1.Image = reader.GetImage(2)
    reader.Close
    btnPic2.Image = btnPic1.Image
    btnPic3.Image = btnPic1.Image
    btnPic4.Image = btnPic1.Image
    btnPic5.Image = btnPic1.Image

    Easy workaround is to add "btnPic1.Image = btnPic2.Image" at the end, but why does it not show the first time?

    If I replace the first button by an Image control, it shows perfectly. It's no big deal, but curious! Am I doing something wrong?

    Geoff.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Does it happen if you don't pass the image to the other controls?
    Code:
    cmd.CommandText = "SELECT * FROM FileStore WHERE Name = 'camera_pic'" 
    reader.Value = cmd.ExecuteReader
    reader.ReadNextRow
    btnPic1.Image = reader.GetImage(
    2)    
    reader.Close
     
  3. glook

    glook Member Licensed User

    Yes, button-1 shows as before - just a a red cross (no text either), while of course the other buttons just correctly show the text only.

    The other buttons also show as a red cross, if I load them directly.
    reader.ReadNextRow
    btnPic1.Image = reader.GetImage(2) ' load image into button
    btnPic2.Image = reader.GetImage(2) ' load image into button
    reader.Close

    Interestingly, if I first load into an ImageList control, it makes no difference and buttons still show as a red cross. Whereas if an invisible Image control is used first, everything works fine. Also OK if an Image control is loaded from the ImageList, like this:-
    reader.ReadNextRow
    ilPMv.Add(reader.GetImage(2)) reader.Close
    btnPic1.Image = ilPMv.Item(0) ' no good - red cross
    imgTest.Image = ilPMv.Item(0) ' works fine

    BTW I'm running 5.80

    Geoff.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    There is a difference between how images pass in different scenarios.
    When you pass an image from one control to another, the image is copied.
    When you pass an image from reader.GetImage to a control or an ImageList it is passed by reference (no new image).
    I'm not sure why there is a difference between the behavior of Image control and ImageButton control.
    Another workaround is:
    Code:
    btnPic1.Image = reader.GetImage(2)    ' load image into butto
    btnPic1.Image = btnPic1.Image
    btnPic2.Image = reader.GetImage(
    2)    ' load image into button
    btnPic2.Image = btnPic2.Image
     
  5. alfcen

    alfcen Well-Known Member Licensed User

    Hi Geoff,
    Please uncheck Transparent for the Image Buttons which reference to an image in a binary file.
    Cheers
    Robert
     
  6. glook

    glook Member Licensed User

    Thanks Erel and Robert. Very helpful explanation of the behaviour. Unchecking Tranparent does indeed allow the image to display - not something I had thought of and a very useful tip for the future. In this particular example, Transparent is used intentionally so the workaround of re-assigning the image gives an effective result. :)

    Geoff.
     
Loading...