Results 1 to 5 of 5

Thread: Save image/bitmap into Access Memo w/o going to HDD [RESOLVED]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Posts
    22

    Question Save image/bitmap into Access Memo w/o going to HDD [RESOLVED]

    I am creating a bitmap/image in my app and want to store it into a memo field in Access table. I can write the bitmap to the HDD as a bmp file, and then read it and insert it into memo field using APPENDCHUNK.

    I WANT TO AVOID CREATING A FILE ON HDD.

    Also, need a way to retrieve this data back from the database, to redisplay it whenever needed (and to verify the save process ).

    Using:
    VB.NET 2003
    ADODB
    Windows XP
    Last edited by jkothia; Mar 29th, 2004 at 07:10 PM.

  2. #2
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    show us the code.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Posts
    22
    I should have included in the subject matter: Need help.

    This is my implementation of the recommended way I found in documentation for vb.net:

    VB Code:
    1. Dim bm As Bitmap = New Bitmap(100, 100)
    2. Dim g As Graphics
    3. g = Graphics.FromImage(bm)
    4. g.Clear(Color.White)
    5. Dim fn As New System.Drawing.Font("Arial", 10, FontStyle.Regular)
    6. Dim drawBrush As New SolidBrush(Color.Black)
    7. g.DrawString("Hello, World", fn, drawBrush, 10, 10)
    8. bm.Save("c:\temp\hw.bmp")
    9. 'read bmp into byte array
    10. Dim img As Image = bm
    11. Dim mst As MemoryStream = New MemoryStream
    12. img.Save(mst, System.Drawing.Imaging.ImageFormat.Bmp)
    13. mst.Flush()
    14.  
    15. Dim byteA() As Byte
    16. byteA = mst.GetBuffer
    17. Dim CONNSTR As String = "Provider=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:\TEMP\DB1.MDB"
    18. Dim conn As Connection = New Connection
    19. conn.Open(CONNSTR)
    20. Dim rsWrite As ADODB.Recordset = New Recordset
    21. 'bmp is a table in db1.mb with bmp field as memo and len_bmp as number
    22. rsWrite.Open("SELECT * FROM BMP", conn, CursorTypeEnum.adOpenDynamic, LockTypeEnum.adLockOptimistic)
    23. rsWrite.AddNew()
    24. rsWrite("BMP").AppendChunk(byteA)
    25. rsWrite("LEN_BMP").Value = byteA.Length
    26. rsWrite.Update()
    27. rsWrite.MoveFirst()
    28.  
    29. rsWrite.Close()
    30. rsWrite.Open("SELECT * FROM BMP", conn)
    31. rsWrite.MoveFirst()
    32. Dim byteB() As Byte = rsWrite("BMP").GetChunk(rsWrite("LEN_BMP").Value)

    I get following exception at the last Dim byteB statement.

    "An unhandled exception of type 'System.InvalidCastException' occurred in WindowsApplication6.exe

    Additional information: Specified cast is not valid"

    Apparently GetChunk returns String (or Object?) and conversion to byte() is needed. Don't know how!. HELP!!!

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Posts
    22

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2004
    Posts
    22

    Sorry, I closed the other thread without really posting code.

    This code snipped requires button1, button2, picturebox1, picturebox2, a Microsoft Access database c:\temp\db1.mdb with an EMPTY table bmp with column bmp as OLE Object with a primary key column.

    Clicking on Button1 creates a bmp/image, stores it in database, and shows it in picturebox1.

    Button2 retrieves the same image and shows it in pictureBox2.

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim ssql As String
    3.         Dim sConn As String
    4.         Dim objDA As OleDbDataAdapter
    5.         Dim objDS As New System.Data.DataSet
    6.         Dim objRow As Data.DataRow
    7.         Dim objDt As Data.DataTable
    8.         Dim objcb As OleDbCommandBuilder
    9.  
    10.         ' create an image in memory or use      Me.PictureBox1.Image = Image.FromFile(fromwhereever)
    11.         Dim bmp As Bitmap = New Bitmap(100, 100)
    12.         Dim g As Graphics = Graphics.FromImage(bmp)
    13.         Dim fn As Font = New Font("Arial", 10, FontStyle.Regular)
    14.         Dim br As New SolidBrush(Color.Black)
    15.         g.Clear(Color.White)
    16.         g.DrawString("Hello, World", fn, br, 0, 0)
    17.         Me.PictureBox1.Image = bmp
    18.  
    19.         Dim mst As System.IO.MemoryStream = New MemoryStream
    20.         Me.PictureBox1.Image.Save(mst, System.Drawing.Imaging.ImageFormat.Bmp)
    21.         Dim mydata() As Byte
    22.         mydata = mst.GetBuffer
    23.  
    24.         ' the database part
    25.         sConn = "Provider=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:\TEMP\DB1.MDB"
    26.         ssql = "SELECT * FROM bmp"
    27.         objDA = New OleDbDataAdapter(ssql, sConn)
    28.         objcb = New OleDbCommandBuilder(objDA)
    29.         objDA.Fill(objDS, "BMP")
    30.         objDt = objDS.Tables("BMP")
    31.         objRow = objDt.NewRow()
    32.         objRow("BMP") = mydata
    33.         objDS.Tables("BMP").Rows.Add(objRow)
    34.  
    35.         objDA.Update(objDS, "BMP")
    36.     End Sub
    37.    
    38.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    39.         Dim ssql As String
    40.         Dim sConn As String
    41.         Dim objDA As OleDbDataAdapter
    42.         Dim objDS As New System.Data.DataSet
    43.         Dim objRow As Data.DataRow
    44.         Dim objDt As Data.DataTable
    45.         Dim objcb As OleDbCommandBuilder
    46.  
    47.         sConn = "Provider=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:\TEMP\DB1.MDB"
    48.         ssql = "SELECT * FROM bmp"
    49.         objDA = New OleDbDataAdapter(ssql, sConn)
    50.         objcb = New OleDbCommandBuilder(objDA)
    51.         objDA.Fill(objDS, "BMP")
    52.         objDt = objDS.Tables("BMP")
    53.         objRow = objDt.Rows(0)
    54.         Dim b() As Byte
    55.         b = objRow("BMP")
    56.  
    57.         Dim mst As MemoryStream = New MemoryStream
    58.         mst.Write(b, 0, b.Length - 1)
    59.         Me.PictureBox2.Image = Image.FromStream(mst)
    60.  
    61.  
    62.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width