|
-
Mar 26th, 2004, 05:32 PM
#1
Thread Starter
Junior Member
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.
-
Mar 26th, 2004, 11:16 PM
#2
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
-
Mar 27th, 2004, 01:30 PM
#3
Thread Starter
Junior Member
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:
Dim bm As Bitmap = New Bitmap(100, 100)
Dim g As Graphics
g = Graphics.FromImage(bm)
g.Clear(Color.White)
Dim fn As New System.Drawing.Font("Arial", 10, FontStyle.Regular)
Dim drawBrush As New SolidBrush(Color.Black)
g.DrawString("Hello, World", fn, drawBrush, 10, 10)
bm.Save("c:\temp\hw.bmp")
'read bmp into byte array
Dim img As Image = bm
Dim mst As MemoryStream = New MemoryStream
img.Save(mst, System.Drawing.Imaging.ImageFormat.Bmp)
mst.Flush()
Dim byteA() As Byte
byteA = mst.GetBuffer
Dim CONNSTR As String = "Provider=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:\TEMP\DB1.MDB"
Dim conn As Connection = New Connection
conn.Open(CONNSTR)
Dim rsWrite As ADODB.Recordset = New Recordset
'bmp is a table in db1.mb with bmp field as memo and len_bmp as number
rsWrite.Open("SELECT * FROM BMP", conn, CursorTypeEnum.adOpenDynamic, LockTypeEnum.adLockOptimistic)
rsWrite.AddNew()
rsWrite("BMP").AppendChunk(byteA)
rsWrite("LEN_BMP").Value = byteA.Length
rsWrite.Update()
rsWrite.MoveFirst()
rsWrite.Close()
rsWrite.Open("SELECT * FROM BMP", conn)
rsWrite.MoveFirst()
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!!!
-
Mar 29th, 2004, 07:09 PM
#4
Thread Starter
Junior Member
-
Mar 30th, 2004, 10:56 AM
#5
Thread Starter
Junior Member
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ssql As String
Dim sConn As String
Dim objDA As OleDbDataAdapter
Dim objDS As New System.Data.DataSet
Dim objRow As Data.DataRow
Dim objDt As Data.DataTable
Dim objcb As OleDbCommandBuilder
' create an image in memory or use Me.PictureBox1.Image = Image.FromFile(fromwhereever)
Dim bmp As Bitmap = New Bitmap(100, 100)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim fn As Font = New Font("Arial", 10, FontStyle.Regular)
Dim br As New SolidBrush(Color.Black)
g.Clear(Color.White)
g.DrawString("Hello, World", fn, br, 0, 0)
Me.PictureBox1.Image = bmp
Dim mst As System.IO.MemoryStream = New MemoryStream
Me.PictureBox1.Image.Save(mst, System.Drawing.Imaging.ImageFormat.Bmp)
Dim mydata() As Byte
mydata = mst.GetBuffer
' the database part
sConn = "Provider=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:\TEMP\DB1.MDB"
ssql = "SELECT * FROM bmp"
objDA = New OleDbDataAdapter(ssql, sConn)
objcb = New OleDbCommandBuilder(objDA)
objDA.Fill(objDS, "BMP")
objDt = objDS.Tables("BMP")
objRow = objDt.NewRow()
objRow("BMP") = mydata
objDS.Tables("BMP").Rows.Add(objRow)
objDA.Update(objDS, "BMP")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ssql As String
Dim sConn As String
Dim objDA As OleDbDataAdapter
Dim objDS As New System.Data.DataSet
Dim objRow As Data.DataRow
Dim objDt As Data.DataTable
Dim objcb As OleDbCommandBuilder
sConn = "Provider=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:\TEMP\DB1.MDB"
ssql = "SELECT * FROM bmp"
objDA = New OleDbDataAdapter(ssql, sConn)
objcb = New OleDbCommandBuilder(objDA)
objDA.Fill(objDS, "BMP")
objDt = objDS.Tables("BMP")
objRow = objDt.Rows(0)
Dim b() As Byte
b = objRow("BMP")
Dim mst As MemoryStream = New MemoryStream
mst.Write(b, 0, b.Length - 1)
Me.PictureBox2.Image = Image.FromStream(mst)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|