473,703 Members | 2,472 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copy/Paste Image from Clipboard

Hello All,

I¡¯m using a context menu associated with some pictureboxes to provide
copy/paste functionality. Copying the image to the clipboard was easy. But
pasting an image from the clipboard is proving to be more difficult. These
pictureboxes are bound to an AccessDB. If the user wants to add an image,
they select an image using an OpenFileDialog:

Dim result As DialogResult = Pic_Sel.ShowDia log()
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.Leng th))
fs.Close()
Table.CurrentRo w.Item("Pic1") = rawData
End If

As you can see, I'm not placing the image directly into the picturebox (ie
PB1.image=image .fromfile). I'm putting the image directly into the data
table, then letting the picturebox update from the table. For some reason,
if I place the image directly into the picturebox and let the picturebox
update the table, I get a GDI+ error if I try to save the image from the
table back to a file.

Anyway, I need to do something similar when I do a GetDataObject from the
clipboard. Any ideas?

TIA,
Lee
Nov 21 '05 #1
7 11625
Hi,

Here is a quick example. Loads the northwind databases category
names into a listbox (listbox1) and displays the image in a picture box
(picturebox1).

Dim ds As DataSet

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim strConn As String

Dim conn As SqlClient.SqlCo nnection

Dim daCustomer As SqlClient.SqlDa taAdapter

ds = New DataSet
strConn = "Server = " + Environment.Mac hineName + "\VSdotNet; "

strConn &= "Database = NorthWind;"

strConn &= "Integrated Security = SSPI;"

conn = New SqlClient.SqlCo nnection(strCon n)

daCustomer = New SqlClient.SqlDa taAdapter("Sele ct * from Categories", conn)

ds = New DataSet

daCustomer.Fill (ds, "Categories ")

ListBox1.DataSo urce = ds.Tables("Cate gories")

ListBox1.Displa yMember = "CategoryNa me"

End Sub

Private Sub ListBox1_Select edValueChanged( ByVal sender As Object, ByVal e As
System.EventArg s) Handles ListBox1.Select edValueChanged

Dim dr As DataRow = ds.Tables("Cate gories").Rows(L istBox1.Selecte dIndex)

Dim ms As New System.IO.Memor yStream

Dim bm As Bitmap

Dim arData() As Byte = dr.Item("Pictur e")

ms.Write(arData , 78, arData.Length - 78)

bm = New Bitmap(ms)

PictureBox1.Ima ge = bm

End Sub

Ken

-----------------

"lgbjr" <lg***@online.n ospam> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hello All,

I¡¯m using a context menu associated with some pictureboxes to provide
copy/paste functionality. Copying the image to the clipboard was easy. But
pasting an image from the clipboard is proving to be more difficult. These
pictureboxes are bound to an AccessDB. If the user wants to add an image,
they select an image using an OpenFileDialog:

Dim result As DialogResult = Pic_Sel.ShowDia log()
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.Leng th))
fs.Close()
Table.CurrentRo w.Item("Pic1") = rawData
End If

As you can see, I'm not placing the image directly into the picturebox (ie
PB1.image=image .fromfile). I'm putting the image directly into the data
table, then letting the picturebox update from the table. For some reason,
if I place the image directly into the picturebox and let the picturebox
update the table, I get a GDI+ error if I try to save the image from the
table back to a file.

Anyway, I need to do something similar when I do a GetDataObject from the
clipboard. Any ideas?

TIA,
Lee

Nov 21 '05 #2
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.ShowDia log()
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.Leng th))
fs.Close()
M_BTS.CurrentRo w.Item("BTS_Pic 1") = 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(sq l, 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.ExecuteRead er(CommandBehav ior.SequentialA ccess)
reader.Read()
fs = New FileStream(pbfi le, FileMode.OpenOr Create, FileAccess.Writ e)
bw = New BinaryWriter(fs )
startIndex = 0
retval = reader.GetBytes (0, 0, outbyte, 0, bufferSize)
bw.Write(outbyt e)
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.SourceContr ol Is Pic1 Then
Clipboard.SetDa taObject(Pic1.I mage, False)
ElseIf CM1.SourceContr ol 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.SourceContr ol Is Pic1 Then
M_BTS.CurrentRo w.Item("BTS_PIC 1") =
Clipboard.GetDa taObject.GetDat a(DataFormats.B itmap, True)
ElseIf CM1.SourceContr ol 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

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:e$******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

Here is a quick example. Loads the northwind databases category
names into a listbox (listbox1) and displays the image in a picture box
(picturebox1).

Dim ds As DataSet

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim strConn As String

Dim conn As SqlClient.SqlCo nnection

Dim daCustomer As SqlClient.SqlDa taAdapter

ds = New DataSet
strConn = "Server = " + Environment.Mac hineName + "\VSdotNet; "

strConn &= "Database = NorthWind;"

strConn &= "Integrated Security = SSPI;"

conn = New SqlClient.SqlCo nnection(strCon n)

daCustomer = New SqlClient.SqlDa taAdapter("Sele ct * from Categories",
conn)

ds = New DataSet

daCustomer.Fill (ds, "Categories ")

ListBox1.DataSo urce = ds.Tables("Cate gories")

ListBox1.Displa yMember = "CategoryNa me"

End Sub

Private Sub ListBox1_Select edValueChanged( ByVal sender As Object, ByVal e
As
System.EventArg s) Handles ListBox1.Select edValueChanged

Dim dr As DataRow = ds.Tables("Cate gories").Rows(L istBox1.Selecte dIndex)

Dim ms As New System.IO.Memor yStream

Dim bm As Bitmap

Dim arData() As Byte = dr.Item("Pictur e")

ms.Write(arData , 78, arData.Length - 78)

bm = New Bitmap(ms)

PictureBox1.Ima ge = bm

End Sub

Ken

-----------------

"lgbjr" <lg***@online.n ospam> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hello All,

I¡¯m using a context menu associated with some pictureboxes to provide
copy/paste functionality. Copying the image to the clipboard was easy. But
pasting an image from the clipboard is proving to be more difficult. These
pictureboxes are bound to an AccessDB. If the user wants to add an image,
they select an image using an OpenFileDialog:

Dim result As DialogResult = Pic_Sel.ShowDia log()
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.Leng th))
fs.Close()
Table.CurrentRo w.Item("Pic1") = rawData
End If

As you can see, I'm not placing the image directly into the picturebox (ie
PB1.image=image .fromfile). I'm putting the image directly into the data
table, then letting the picturebox update from the table. For some reason,
if I place the image directly into the picturebox and let the picturebox
update the table, I get a GDI+ error if I try to save the image from the
table back to a file.

Anyway, I need to do something similar when I do a GetDataObject from the
clipboard. Any ideas?

TIA,
Lee

Nov 21 '05 #3
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(ByVa l bm As Bitmap) As Object

Dim ms As New MemoryStream

Try

bm.Save(ms, Imaging.ImageFo rmat.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.Memor yStream

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/de...ssavetopic.asp

http://support.microsoft.com/default...b;en-us;818410

Picture box control that I overloaded to support binding to a database.
http://www.gotdotnet.com/Community/U...3-c664ae2f91f8
Ken

----------------------
"lgbjr" <lg***@online.n ospam> wrote in message
news:eX******** ********@TK2MSF TNGP09.phx.gbl. ..
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.ShowDia log()
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.Leng th))
fs.Close()
M_BTS.CurrentRo w.Item("BTS_Pic 1") = 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(sq l, 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.ExecuteRead er(CommandBehav ior.SequentialA ccess)
reader.Read()
fs = New FileStream(pbfi le, FileMode.OpenOr Create, FileAccess.Writ e)
bw = New BinaryWriter(fs )
startIndex = 0
retval = reader.GetBytes (0, 0, outbyte, 0, bufferSize)
bw.Write(outbyt e)
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.SourceContr ol Is Pic1 Then
Clipboard.SetDa taObject(Pic1.I mage, False)
ElseIf CM1.SourceContr ol 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.SourceContr ol Is Pic1 Then
M_BTS.CurrentRo w.Item("BTS_PIC 1") =
Clipboard.GetDa taObject.GetDat a(DataFormats.B itmap, True)
ElseIf CM1.SourceContr ol 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

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:e$******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

Here is a quick example. Loads the northwind databases category
names into a listbox (listbox1) and displays the image in a picture box
(picturebox1).

Dim ds As DataSet

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim strConn As String

Dim conn As SqlClient.SqlCo nnection

Dim daCustomer As SqlClient.SqlDa taAdapter

ds = New DataSet
strConn = "Server = " + Environment.Mac hineName + "\VSdotNet; "

strConn &= "Database = NorthWind;"

strConn &= "Integrated Security = SSPI;"

conn = New SqlClient.SqlCo nnection(strCon n)

daCustomer = New SqlClient.SqlDa taAdapter("Sele ct * from Categories",
conn)

ds = New DataSet

daCustomer.Fill (ds, "Categories ")

ListBox1.DataSo urce = ds.Tables("Cate gories")

ListBox1.Displa yMember = "CategoryNa me"

End Sub

Private Sub ListBox1_Select edValueChanged( ByVal sender As Object, ByVal e
As
System.EventArg s) Handles ListBox1.Select edValueChanged

Dim dr As DataRow = ds.Tables("Cate gories").Rows(L istBox1.Selecte dIndex)

Dim ms As New System.IO.Memor yStream

Dim bm As Bitmap

Dim arData() As Byte = dr.Item("Pictur e")

ms.Write(arData , 78, arData.Length - 78)

bm = New Bitmap(ms)

PictureBox1.Ima ge = bm

End Sub

Ken

-----------------

"lgbjr" <lg***@online.n ospam> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hello All,

I¡¯m using a context menu associated with some pictureboxes to provide
copy/paste functionality. Copying the image to the clipboard was easy. But
pasting an image from the clipboard is proving to be more difficult. These
pictureboxes are bound to an AccessDB. If the user wants to add an image,
they select an image using an OpenFileDialog:

Dim result As DialogResult = Pic_Sel.ShowDia log()
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.Leng th))
fs.Close()
Table.CurrentRo w.Item("Pic1") = rawData
End If

As you can see, I'm not placing the image directly into the picturebox (ie
PB1.image=image .fromfile). I'm putting the image directly into the data
table, then letting the picturebox update from the table. For some reason,
if I place the image directly into the picturebox and let the picturebox
update the table, I get a GDI+ error if I try to save the image from the
table back to a file.

Anyway, I need to do something similar when I do a GetDataObject from the
clipboard. Any ideas?

TIA,
Lee


Nov 21 '05 #4
Ken,

Again, thanks for the reply, but also again, I think there's a
misunderstandin g. Maybe I'm just not being clear about what I am having
trouble with, or I'm too dense to understand what your saying!

I understand that I don't "need" the clipboard for displaying images from a
DB. If the user wants to place an image in the DB, they click an
open image button and using the code from the previous post, I stream the
image into the DB. Once it's there, the picturebox displays it.

If the user wants to retrieve a copy of an image in the DB (ie. a disk
file), they click a save to disk button and I use the code in the previous
post to stream the image to a disk file. If the original image had MetaData
associated with it, the image that is saved to disk has the same MetaData.

Both of these work with no problems.

Now, if the user doesn't want/need a disk file, but wants to place a copy of
one
of the images in the DB in another app (let's say a Word Document), again, I
use the code from the previous post (in a context menu) to put a copy of the
image from the picturebox (not from the DB) onto the clipboard. The user
goes to the target app and clicks paste. I understand that in this case, any
MetaData associated with the image is not included on the clipboard (which
is ok). I actually used the info from
http://support.microsoft.com/default...b;en-us;818410 to verify
that what I'm copying to the clipboard is a valid image (by saving the
contents of the clipboard to a file. The saved image file is exactly the
same as the original image file (minus any MetaData)

So, This also works with no problems.

If the user wants to add an image to the DB and there isn't an image file,
ie.
they want to copy an image from another App (again, let's say Word), and
paste it into a picturebox, using the code in my previous post, I'm pasting
the contents of the clipboard into the DB. This is where the problem is.
Whether I paste the image directly to the picturebox
(Pic1.Image=Cli pboard.GetDataO bject.GetData(D ataFormats.Bitm ap, True)) or
paste the image directly to the table as shown in the code below, the image
is corrupted (See previous post for description of corrupted images). I've
also tried setting the AutoConvert flag to False for both methods. Same
problem.

I'm assuming that I need to do something similar to how I store an image in
the DB from disk. I use a filestream to fill a byte array, then store the
byte array directly to the DB, then let the picturebox display the image
from the DB, rather than using Pic1.Image=Imag e.Fromfile and letting the
picturebox store the image to the DB. The later method causes GDI+ errors if
I subsequently try to save the image from the DB to a file.

So, I guess what I need to know is how to get the contents of the clipboard
into a byte array, so I can store the byte array directly to the DB, and
then let the picbox display the image.

Sorry for any previous confusion!

TIA,
Lee

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:OF******** ******@TK2MSFTN GP12.phx.gbl...
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(ByVa l bm As Bitmap) As Object

Dim ms As New MemoryStream

Try

bm.Save(ms, Imaging.ImageFo rmat.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.Memor yStream

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/de...ssavetopic.asp

http://support.microsoft.com/default...b;en-us;818410

Picture box control that I overloaded to support binding to a database.
http://www.gotdotnet.com/Community/U...3-c664ae2f91f8
Ken

----------------------
"lgbjr" <lg***@online.n ospam> wrote in message
news:eX******** ********@TK2MSF TNGP09.phx.gbl. ..
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.ShowDia log()
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.Leng th))
fs.Close()
M_BTS.CurrentRo w.Item("BTS_Pic 1") = 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(sq l, 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.ExecuteRead er(CommandBehav ior.SequentialA ccess)
reader.Read()
fs = New FileStream(pbfi le, FileMode.OpenOr Create, FileAccess.Writ e)
bw = New BinaryWriter(fs )
startIndex = 0
retval = reader.GetBytes (0, 0, outbyte, 0, bufferSize)
bw.Write(outbyt e)
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.SourceContr ol Is Pic1 Then
Clipboard.SetDa taObject(Pic1.I mage, False)
ElseIf CM1.SourceContr ol 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.SourceContr ol Is Pic1 Then
M_BTS.CurrentRo w.Item("BTS_PIC 1") =
Clipboard.GetDa taObject.GetDat a(DataFormats.B itmap, True)
ElseIf CM1.SourceContr ol 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

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:e$******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

Here is a quick example. Loads the northwind databases category
names into a listbox (listbox1) and displays the image in a picture box
(picturebox1).

Dim ds As DataSet

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim strConn As String

Dim conn As SqlClient.SqlCo nnection

Dim daCustomer As SqlClient.SqlDa taAdapter

ds = New DataSet
strConn = "Server = " + Environment.Mac hineName + "\VSdotNet; "

strConn &= "Database = NorthWind;"

strConn &= "Integrated Security = SSPI;"

conn = New SqlClient.SqlCo nnection(strCon n)

daCustomer = New SqlClient.SqlDa taAdapter("Sele ct * from Categories",
conn)

ds = New DataSet

daCustomer.Fill (ds, "Categories ")

ListBox1.DataSo urce = ds.Tables("Cate gories")

ListBox1.Displa yMember = "CategoryNa me"

End Sub

Private Sub ListBox1_Select edValueChanged( ByVal sender As Object, ByVal e
As
System.EventArg s) Handles ListBox1.Select edValueChanged

Dim dr As DataRow = ds.Tables("Cate gories").Rows(L istBox1.Selecte dIndex)

Dim ms As New System.IO.Memor yStream

Dim bm As Bitmap

Dim arData() As Byte = dr.Item("Pictur e")

ms.Write(arData , 78, arData.Length - 78)

bm = New Bitmap(ms)

PictureBox1.Ima ge = bm

End Sub

Ken

-----------------

"lgbjr" <lg***@online.n ospam> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hello All,

I¡¯m using a context menu associated with some pictureboxes to provide
copy/paste functionality. Copying the image to the clipboard was easy.
But
pasting an image from the clipboard is proving to be more difficult.
These
pictureboxes are bound to an AccessDB. If the user wants to add an image,
they select an image using an OpenFileDialog:

Dim result As DialogResult = Pic_Sel.ShowDia log()
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.Leng th))
fs.Close()
Table.CurrentRo w.Item("Pic1") = rawData
End If

As you can see, I'm not placing the image directly into the picturebox
(ie
PB1.image=image .fromfile). I'm putting the image directly into the data
table, then letting the picturebox update from the table. For some
reason,
if I place the image directly into the picturebox and let the picturebox
update the table, I get a GDI+ error if I try to save the image from the
table back to a file.

Anyway, I need to do something similar when I do a GetDataObject from the
clipboard. Any ideas?

TIA,
Lee



Nov 21 '05 #5
lgbjr,

I did not check it, however probably is what you are looking in an object.

Why don't you set a breakpoint there (after) the point you have that object.

Open the quickwatch and sees how that image (most probably a byte array) is
stored in that object?

That is the way I would do this.

I hope this gives some ideas?

Cor
Nov 21 '05 #6
Ken,

You can ignore my last post. I was obviously too dense to understand what
you were trying to say. I used the storeimage function and passed it the
contents of the clipboard, then saved the return value to the DB. Works
fine!!

Thanks!!

Lee

"lgbjr" <lg***@online.n ospam> wrote in message
news:uI******** ******@TK2MSFTN GP14.phx.gbl...
Ken,

Again, thanks for the reply, but also again, I think there's a
misunderstandin g. Maybe I'm just not being clear about what I am having
trouble with, or I'm too dense to understand what your saying!

I understand that I don't "need" the clipboard for displaying images from
a
DB. If the user wants to place an image in the DB, they click an
open image button and using the code from the previous post, I stream the
image into the DB. Once it's there, the picturebox displays it.

If the user wants to retrieve a copy of an image in the DB (ie. a disk
file), they click a save to disk button and I use the code in the previous
post to stream the image to a disk file. If the original image had
MetaData associated with it, the image that is saved to disk has the same
MetaData.

Both of these work with no problems.

Now, if the user doesn't want/need a disk file, but wants to place a copy
of one
of the images in the DB in another app (let's say a Word Document), again,
I
use the code from the previous post (in a context menu) to put a copy of
the image from the picturebox (not from the DB) onto the clipboard. The
user goes to the target app and clicks paste. I understand that in this
case, any MetaData associated with the image is not included on the
clipboard (which is ok). I actually used the info from
http://support.microsoft.com/default...b;en-us;818410 to verify
that what I'm copying to the clipboard is a valid image (by saving the
contents of the clipboard to a file. The saved image file is exactly the
same as the original image file (minus any MetaData)

So, This also works with no problems.

If the user wants to add an image to the DB and there isn't an image file,
ie.
they want to copy an image from another App (again, let's say Word), and
paste it into a picturebox, using the code in my previous post, I'm
pasting
the contents of the clipboard into the DB. This is where the problem is.
Whether I paste the image directly to the picturebox
(Pic1.Image=Cli pboard.GetDataO bject.GetData(D ataFormats.Bitm ap, True)) or
paste the image directly to the table as shown in the code below, the
image
is corrupted (See previous post for description of corrupted images). I've
also tried setting the AutoConvert flag to False for both methods. Same
problem.

I'm assuming that I need to do something similar to how I store an image
in the DB from disk. I use a filestream to fill a byte array, then store
the byte array directly to the DB, then let the picturebox display the
image from the DB, rather than using Pic1.Image=Imag e.Fromfile and letting
the picturebox store the image to the DB. The later method causes GDI+
errors if I subsequently try to save the image from the DB to a file.

So, I guess what I need to know is how to get the contents of the
clipboard into a byte array, so I can store the byte array directly to the
DB, and then let the picbox display the image.

Sorry for any previous confusion!

TIA,
Lee

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:OF******** ******@TK2MSFTN GP12.phx.gbl...
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(ByVa l bm As Bitmap) As Object

Dim ms As New MemoryStream

Try

bm.Save(ms, Imaging.ImageFo rmat.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.Memor yStream

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/de...ssavetopic.asp

http://support.microsoft.com/default...b;en-us;818410

Picture box control that I overloaded to support binding to a database.
http://www.gotdotnet.com/Community/U...3-c664ae2f91f8
Ken

----------------------
"lgbjr" <lg***@online.n ospam> wrote in message
news:eX******** ********@TK2MSF TNGP09.phx.gbl. ..
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.ShowDia log()
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.Leng th))
fs.Close()
M_BTS.CurrentRo w.Item("BTS_Pic 1") = 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(sq l, 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.ExecuteRead er(CommandBehav ior.SequentialA ccess)
reader.Read()
fs = New FileStream(pbfi le, FileMode.OpenOr Create, FileAccess.Writ e)
bw = New BinaryWriter(fs )
startIndex = 0
retval = reader.GetBytes (0, 0, outbyte, 0, bufferSize)
bw.Write(outbyt e)
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.SourceContr ol Is Pic1 Then
Clipboard.SetDa taObject(Pic1.I mage, False)
ElseIf CM1.SourceContr ol 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.SourceContr ol Is Pic1 Then
M_BTS.CurrentRo w.Item("BTS_PIC 1") =
Clipboard.GetDa taObject.GetDat a(DataFormats.B itmap, True)
ElseIf CM1.SourceContr ol 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

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:e$******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

Here is a quick example. Loads the northwind databases category
names into a listbox (listbox1) and displays the image in a picture box
(picturebox1).

Dim ds As DataSet

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim strConn As String

Dim conn As SqlClient.SqlCo nnection

Dim daCustomer As SqlClient.SqlDa taAdapter

ds = New DataSet
strConn = "Server = " + Environment.Mac hineName + "\VSdotNet; "

strConn &= "Database = NorthWind;"

strConn &= "Integrated Security = SSPI;"

conn = New SqlClient.SqlCo nnection(strCon n)

daCustomer = New SqlClient.SqlDa taAdapter("Sele ct * from Categories",
conn)

ds = New DataSet

daCustomer.Fill (ds, "Categories ")

ListBox1.DataSo urce = ds.Tables("Cate gories")

ListBox1.Displa yMember = "CategoryNa me"

End Sub

Private Sub ListBox1_Select edValueChanged( ByVal sender As Object, ByVal
e
As
System.EventArg s) Handles ListBox1.Select edValueChanged

Dim dr As DataRow = ds.Tables("Cate gories").Rows(L istBox1.Selecte dIndex)

Dim ms As New System.IO.Memor yStream

Dim bm As Bitmap

Dim arData() As Byte = dr.Item("Pictur e")

ms.Write(arData , 78, arData.Length - 78)

bm = New Bitmap(ms)

PictureBox1.Ima ge = bm

End Sub

Ken

-----------------

"lgbjr" <lg***@online.n ospam> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Hello All,

I¡¯m using a context menu associated with some pictureboxes to provide
copy/paste functionality. Copying the image to the clipboard was easy.
But
pasting an image from the clipboard is proving to be more difficult.
These
pictureboxes are bound to an AccessDB. If the user wants to add an
image,
they select an image using an OpenFileDialog:

Dim result As DialogResult = Pic_Sel.ShowDia log()
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.Leng th))
fs.Close()
Table.CurrentRo w.Item("Pic1") = rawData
End If

As you can see, I'm not placing the image directly into the picturebox
(ie
PB1.image=image .fromfile). I'm putting the image directly into the data
table, then letting the picturebox update from the table. For some
reason,
if I place the image directly into the picturebox and let the picturebox
update the table, I get a GDI+ error if I try to save the image from the
table back to a file.

Anyway, I need to do something similar when I do a GetDataObject from
the
clipboard. Any ideas?

TIA,
Lee



Nov 21 '05 #7
Hi Cor,

Thanks for the idea. I was actually in the process of setting breakpoints
and trying to look at the byte array when I got the post from Ken. At first,
I didn't grasp what he was saying, so I sent another post trying to explain
again what I was trying to do. then I took a 5 minute coffee break! when I
sat back down in front of the computer and thought about it, I realized that
Ken sent me the answer I was looking for. I was just to dense to see it when
I first read his post. So, all's well now.

To retrieve an image that's been placed on the clipboard directly to the DB:

Table.CurrentRo w.Item("Pic1")= StoreImage(Clip board.GetDataOb ject.GetData(Da taFormats.Bitma p,
False))

Works like a charm!

cheers,
Lee

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
lgbjr,

I did not check it, however probably is what you are looking in an object.

Why don't you set a breakpoint there (after) the point you have that
object.

Open the quickwatch and sees how that image (most probably a byte array)
is stored in that object?

That is the way I would do this.

I hope this gives some ideas?

Cor

Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2125
by: TC | last post by:
Hello, Here is what I'm trying to do: -- Make sure both MS Excel and MS Word are running -- Create an Excel chart -- Save the Excel file -- Copy the Excel chart onto the clipboard using Ctrl + C -- Go to Word and look under Edit > Paste Special -- Note there is a source reference and an option to paste the chart as a
5
4069
by: TC | last post by:
Hello, Here is what I'm trying to do: -- Make sure both MS Excel and MS Word are running -- Create an Excel chart -- Save the Excel file -- Copy the Excel chart onto the clipboard using Ctrl + C -- Go to Word and look under Edit > Paste Special -- Note there is a source reference and an option to paste the chart as a
4
32269
by: Risto Heinonen | last post by:
Hi. I have a web page that has images and text. I can carefully select one image and the on the right of the image and then copy & paste to Word. Is it possible to make javascript do the same: select the same area (divided by <div> tag) and copy to clipboard? So far I succeeded to make the script to copy the area inside <div> but it was plain html-text.
3
43364
by: Tor Inge Rislaa | last post by:
Copy, Cut and Paste How to code the Copy, Cut and Paste functionality in VB.NET. In VB 6.0 I used the following code: 'For Copy Clipboard.Clear Clipboard.SetText ActiveForm.ActiveControl.SelText, vbCFText
5
21023
by: DraguVaso | last post by:
Hi, I'm looking for a way to Copy and Paste Files to the clipboard. I found a lot of articles to copy pieces of text and bitmaps etc, but nog whole files. Whay I need is like you have in windows explorer: when you do a right-click on a file and choose Copy, and than paste it somewhere in my application and vice versa.
0
2662
by: mhospodarsky | last post by:
Hi-- I am using VB.Net 2002 for this app. I am working with Tiff and jpeg images. I have a picturebox set up that I use to view the images. I have the picture box inside of a scrollable panel.There are two viewing preferences available: to view the image with scrollbars (the original image unsized), and to view the iamge without scrollbars (the image resized to fit within the panel's viewing range.) When I resize the image so that...
17
5122
by: Steve | last post by:
I'm trying to code cut, copy, and paste in vb 2005 so that when the user clicks on a toolbar button, the cut/copy/paste will work with whatever textbox the cursor is current located in (I have about 20 textboxes on the form). Also to ensure that the button can't get used if the cursor isn't in a textbox field. And to ensure the contents of the clipboard are "text" contents that have been cut/copied from one of the textboxes on the form. ...
8
15437
by: jh | last post by:
I'd like to copy/paste into a listbox during runtime. I can do this for a textbox but can't figure out how to accomplish this for a listbox. Any help? Thanks.
2
3325
by: crapycoder | last post by:
hi all, please help....!! i have added a kodak image edit control on my form. when i click get image button, "abc.img" will be displayed. i have a button named "copy to clipboard". when i click it, i want the image in kodak image edit control to be copied to clipboard.
0
9123
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6594
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5923
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4434
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3125
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2070
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.