Hi,
You dont need to use the clipboard to display an image stored in an
database. Here are two functions to convert an image to and from a format
you can save in a database. Storeimage converts image to binary format.
ConvertToBitmap converts it back. Note the offset for the northwind database
is 78 my storeimage function has a offset of 0. Once you have converted the
image into a bitmap you can use bitmap.save to store the image.
Private Function StoreImage(ByVal bm As Bitmap) As Object
Dim ms As New MemoryStream
Try
bm.Save(ms, Imaging.ImageFormat.Jpeg)
Return ms.GetBuffer
Catch
Return Convert.DBNull
End Try
End Function
Private Function ConvertToBitmap(ByVal data() As Byte, ByVal offset As
Integer) As Bitmap
Dim ms As New System.IO.MemoryStream
Dim bm As Bitmap
ms = New MemoryStream
ms.Write(data, offset, data.Length - offset)
bm = New Bitmap(ms)
Return bm
End Function
Bitmap save
http://msdn.microsoft.com/library/d...tml/frlrfsystemdrawingimageclasssavetopic.asp
http://support.microsoft.com/default.aspx?scid=kb;en-us;818410
Picture box control that I overloaded to support binding to a database.
http://www.gotdotnet.com/Community/...mpleGuid=daf38a9e-0e33-4777-93d3-c664ae2f91f8
Ken
----------------------
Hi Ken,
I'm not sure whether you misunderstood what I was asking, or whether what
you've provided is way over my head (because I'm not sure I understand)!
Anyway, let me give you a bit more information:
I have a form with several pictureboxes (and other controls) that are bound
to an AccessDB. To avoid the nasty "Generic GDI+ Error", I am using the
following code to store an image in the DB:
Dim result As DialogResult = Pic_Sel.ShowDialog()
If (result = DialogResult.OK) Then
Dim fs As FileStream = New FileStream(Pic_Sel.FileName, FileMode.Open,
FileAccess.Read)
Dim rawData() As Byte = New Byte(fs.Length) {}
fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length))
fs.Close()
M_BTS.CurrentRow.Item("BTS_Pic1") = rawData
End If
To retrieve the image from the DB (save it back to disk), I'm using the
following (where pbfile is the disk file):
Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
Dim fs As FileStream
Dim bw As BinaryWriter
Dim bufferSize As Integer = 300000
Dim outbyte(300000 - 1) As Byte
Dim retval As Long
Dim startIndex As Long = 0
Dim reader As OleDbDataReader =
cmd.ExecuteReader(CommandBehavior.SequentialAccess)
reader.Read()
fs = New FileStream(pbfile, FileMode.OpenOrCreate, FileAccess.Write)
bw = New BinaryWriter(fs)
startIndex = 0
retval = reader.GetBytes(0, 0, outbyte, 0, bufferSize)
bw.Write(outbyte)
bw.Flush()
bw.Close()
fs.Close()
reader.Close()
The reason I'm doing this is because if I do a pb.image.save, I get a GDI+
error.
So, now I'm able to get images in/out of the AccessDB, while displaying them
in the pictureboxes.
Now, I'm adding a context menu to the Pictureboxes to allow
open/edit/preview/copy/paste of the images. Open, edit, and preview are no
problem. when I load the form, I'm going to the registry and getting the
information for whatever programs the user's system has defined for opening,
editing, and previewing images and using that in my context menu. For the
copy, I'm doing the following:
If CM1.SourceControl Is Pic1 Then
Clipboard.SetDataObject(Pic1.Image, False)
ElseIf CM1.SourceControl Is Pic2 Then
etc.
I can paste the contents of the clipboard to any app that supports images
(Word, Excel, Outlook, etc.).
For paste (into a picturebox), currently I'm using
If CM1.SourceControl Is Pic1 Then
M_BTS.CurrentRow.Item("BTS_PIC1") =
Clipboard.GetDataObject.GetData(DataFormats.Bitmap, True)
ElseIf CM1.SourceControl Is Pic2 Then
etc.
This is where the problem is. If I copy an image from pb1 and paste it to
pb2, the image in the picturebox looks fine (which means that whatever data
is being put in the table is being properly translated by the picturebox).
But, if I try to open the image (save it to disk, then open it, the file is
corrupted, or at minimum, changed.
some examples:
If I store an image from a digital camera (c.jpg) in the AccessDB, then save
it to disk (c1.jpg), the file is exactly the same as the original (including
the MetaData). So, I know my method for getting images into and from the DB
is working.
If I copy an image from one pb to another pb (let's say c.jpg), update the
DB, then try to open the image (save to disk), the image is corrupted. The
original image was 1024x768x24b. The image created from the pasted data is
1024x768x32b. There is no MetaData. And only about the top 20% of the
original image exists. Additionally, the image type for the original image
is JPEG, the image from the pasted data is PNG.
For a smaller file, say 150x150x24b, all of the same corruption occurs, but
I can see the entire image.
So, I don't know if it's the way I'm copying the image to the clipboard, or
the way I'm pasting it back to my app, or some combination of both. I'm
hoping that it's only the way I'm pasting it, as I can't control the way
other apps copy images to the clipboard.
TIA,
Lee