Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Am I being stupid? How to load an image from SQL and display it as a label background?

Am I being stupid? How to load an image from SQL and display it as a label background?

Scheduled Pinned Locked Moved WCF and WF
databasecsharpcsshelp
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chinners
    wrote on last edited by
    #1

    Hi all, I'm using VB.net2008SP1, framework 3.51 All I want to do is get an image stored in a database and display it as a background on a label uielement in my grid (have I got the terminology right here?) The code I have is:

               Using ms As New IO.MemoryStream
                  With DirectCast(Data, Byte())
                     ms.Write(DirectCast(Data, Byte()), 0, .Length)
                  End With
                  ms.Seek(0, IO.SeekOrigin.Begin)
                  Dim bmi As New BitmapImage
                  bmi.BeginInit()
                  bmi.StreamSource = ms
                  bmi.EndInit()
                  Dim ImageDrawing As New ImageBrush(bmi)
                  iResult = ImageDrawing
               End Using
    

    I then pass iResult to the background property of my label. The result is an empty box - no image, just the frame. If I were to load the image from a file using this code, it works.

         Dim Bmp As New BitmapImage(New Uri(DR("BackgroundImage").ToString))
         Dim BackBrush As New ImageBrush(Bmp)
         MyLabel.Background = BackBrush
    

    Unfortunately, due to the licensing of the imagelibrary I am using, I cannot save these images in a temp folder or anything simple like that. I have tried googling, rummaging around code project, typing random commands, and have just tried a strong coffee, but I cant get it working. Can anyone help me????

    C 1 Reply Last reply
    0
    • C Chinners

      Hi all, I'm using VB.net2008SP1, framework 3.51 All I want to do is get an image stored in a database and display it as a background on a label uielement in my grid (have I got the terminology right here?) The code I have is:

                 Using ms As New IO.MemoryStream
                    With DirectCast(Data, Byte())
                       ms.Write(DirectCast(Data, Byte()), 0, .Length)
                    End With
                    ms.Seek(0, IO.SeekOrigin.Begin)
                    Dim bmi As New BitmapImage
                    bmi.BeginInit()
                    bmi.StreamSource = ms
                    bmi.EndInit()
                    Dim ImageDrawing As New ImageBrush(bmi)
                    iResult = ImageDrawing
                 End Using
      

      I then pass iResult to the background property of my label. The result is an empty box - no image, just the frame. If I were to load the image from a file using this code, it works.

           Dim Bmp As New BitmapImage(New Uri(DR("BackgroundImage").ToString))
           Dim BackBrush As New ImageBrush(Bmp)
           MyLabel.Background = BackBrush
      

      Unfortunately, due to the licensing of the imagelibrary I am using, I cannot save these images in a temp folder or anything simple like that. I have tried googling, rummaging around code project, typing random commands, and have just tried a strong coffee, but I cant get it working. Can anyone help me????

      C Offline
      C Offline
      Chinners
      wrote on last edited by
      #2

      Yes... I was being stupid...

                    bmi.BeginInit()
                    **bmi.CacheOption = BitmapCacheOption.OnLoad**
                    bmi.StreamSource = ms
                    bmi.EndInit()
      

      Setting the cacheoption to be onload sorts the problem out. I guess that the image was being created after the stream was closed (or something along those lines). :doh:

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups