473,385 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Databound PictureBox control

I have created a PictureBox control which can have it's Image property
directly bound to an image field in a database. This works perfectly for
showing the images that are in the database. When I paste a new image into
my control, it shows the new image and updates the image property. Problem
is... It doesn't update the bound field! Any thoughts as to what I've done
wrong? Any help would be greatly appreciated. The code for the Picturebox
and the code that I use to paste the image are below.

Thanks in Advance,
David J. Ricker II

-----------------Start of Picturebox Control

Imports System.ComponentModel

Public Class MyPictureBox
Inherits System.Windows.Forms.PictureBox

#Region " Component Designer generated code "
Public Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()
'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)
End Sub
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
#End Region

Public Event ImageChanged As EventHandler

Private mImage As Object

<Bindable(True), Browsable(True)> Public Shadows Property Image() As
Object
Get
Return mImage
End Get
Set(ByVal Value As Object)
If IsDBNull(Value) Then
Dim g As New System.Drawing.Bitmap(1, 1)
g.SetPixel(0, 0, System.Drawing.Color.White)
MyBase.Image = g
ElseIf Value Is Nothing Then
Dim g As New System.Drawing.Bitmap(1, 1)
g.SetPixel(0, 0, System.Drawing.Color.White)
MyBase.Image = g
ElseIf Value.GetType.FullName = "System.Drawing.Bitmap" Then
MyBase.Image = Value
Dim ms As New System.IO.MemoryStream
CType(Value, Bitmap).Save(ms,
System.Drawing.Imaging.ImageFormat.Jpeg)
Dim bytBLOBData(ms.Length - 1) As Byte
ms.Position = 0
ms.Read(bytBLOBData, 0, ms.Length)
Value = bytBLOBData
Else
Dim img() As Byte = CType(Value, Byte())
Dim ms As New System.IO.MemoryStream
Dim offset As Integer = 0
ms.Write(img, offset, img.Length - offset)
Dim bmp As Bitmap = New Bitmap(ms)
ms.Close()
MyBase.Image = bmp
End If
mImage = Value
RaiseEvent ImageChanged(Me, New System.EventArgs)
End Set
End Property
End Class

-----------------End of Picturebox Control
-----------------Start of Image Pasting

Private Sub btnPasteImage_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPasteImage.Click
Dim data As IDataObject = Clipboard.GetDataObject
If (data.GetDataPresent(DataFormats.Bitmap)) Then
MyPictureBox1.Image = data.GetData(DataFormats.Bitmap)
End If
End Sub

-----------------End of Image Pasting
Nov 20 '05 #1
2 2412
Also,
If my image is in a child record of a master/child relationship, and I
change the visible child record and then save, the image saves. If I don't
change the child record I'm looking at first, then the image doesn't save.

Thanks Again,
David J. Ricker II

"David Ricker" <da***@vanamatic.com> wrote in message
news:Og****************@TK2MSFTNGP10.phx.gbl...
I have created a PictureBox control which can have it's Image property
directly bound to an image field in a database. This works perfectly for
showing the images that are in the database. When I paste a new image into my control, it shows the new image and updates the image property. Problem is... It doesn't update the bound field! Any thoughts as to what I've done wrong? Any help would be greatly appreciated. The code for the Picturebox and the code that I use to paste the image are below.

Thanks in Advance,
David J. Ricker II

-----------------Start of Picturebox Control

Imports System.ComponentModel

Public Class MyPictureBox
Inherits System.Windows.Forms.PictureBox

#Region " Component Designer generated code "
Public Sub New(ByVal Container As System.ComponentModel.IContainer) MyClass.New()
'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)
End Sub
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component Designer 'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
#End Region

Public Event ImageChanged As EventHandler

Private mImage As Object

<Bindable(True), Browsable(True)> Public Shadows Property Image() As
Object
Get
Return mImage
End Get
Set(ByVal Value As Object)
If IsDBNull(Value) Then
Dim g As New System.Drawing.Bitmap(1, 1)
g.SetPixel(0, 0, System.Drawing.Color.White)
MyBase.Image = g
ElseIf Value Is Nothing Then
Dim g As New System.Drawing.Bitmap(1, 1)
g.SetPixel(0, 0, System.Drawing.Color.White)
MyBase.Image = g
ElseIf Value.GetType.FullName = "System.Drawing.Bitmap" Then
MyBase.Image = Value
Dim ms As New System.IO.MemoryStream
CType(Value, Bitmap).Save(ms,
System.Drawing.Imaging.ImageFormat.Jpeg)
Dim bytBLOBData(ms.Length - 1) As Byte
ms.Position = 0
ms.Read(bytBLOBData, 0, ms.Length)
Value = bytBLOBData
Else
Dim img() As Byte = CType(Value, Byte())
Dim ms As New System.IO.MemoryStream
Dim offset As Integer = 0
ms.Write(img, offset, img.Length - offset)
Dim bmp As Bitmap = New Bitmap(ms)
ms.Close()
MyBase.Image = bmp
End If
mImage = Value
RaiseEvent ImageChanged(Me, New System.EventArgs)
End Set
End Property
End Class

-----------------End of Picturebox Control
-----------------Start of Image Pasting

Private Sub btnPasteImage_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPasteImage.Click
Dim data As IDataObject = Clipboard.GetDataObject
If (data.GetDataPresent(DataFormats.Bitmap)) Then
MyPictureBox1.Image = data.GetData(DataFormats.Bitmap)
End If
End Sub

-----------------End of Image Pasting

Nov 20 '05 #2
I have found a workaround to this problem, however I am still curious if
anyone has any ideas as to why the dataset was not being updated in the
first place. The workaround involved manually setting the image in the
dataset to the image value of the control. I have added the following line
of code after I paste the image into my control:

CType(CType(Me.BindingContext(Dataview1, "DataMember").Current,
System.Data.DataRowView).Row, dataset1.TableRow).Image =
Me.MyPictureBox1.Image

Thanks Again,
David J. Ricker II

"David Ricker" <da***@vanamatic.com> wrote in message
news:O$**************@TK2MSFTNGP11.phx.gbl...
Also,
If my image is in a child record of a master/child relationship, and I
change the visible child record and then save, the image saves. If I don't change the child record I'm looking at first, then the image doesn't save.

Thanks Again,
David J. Ricker II

"David Ricker" <da***@vanamatic.com> wrote in message
news:Og****************@TK2MSFTNGP10.phx.gbl...
I have created a PictureBox control which can have it's Image property
directly bound to an image field in a database. This works perfectly for showing the images that are in the database. When I paste a new image

into
my control, it shows the new image and updates the image property.

Problem
is... It doesn't update the bound field! Any thoughts as to what I've

done
wrong? Any help would be greatly appreciated. The code for the

Picturebox
and the code that I use to paste the image are below.

Thanks in Advance,
David J. Ricker II

-----------------Start of Picturebox Control

Imports System.ComponentModel

Public Class MyPictureBox
Inherits System.Windows.Forms.PictureBox

#Region " Component Designer generated code "
Public Sub New(ByVal Container As

System.ComponentModel.IContainer)
MyClass.New()
'Required for Windows.Forms Class Composition Designer support Container.Add(Me)
End Sub
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component

Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
#End Region

Public Event ImageChanged As EventHandler

Private mImage As Object

<Bindable(True), Browsable(True)> Public Shadows Property Image() As
Object
Get
Return mImage
End Get
Set(ByVal Value As Object)
If IsDBNull(Value) Then
Dim g As New System.Drawing.Bitmap(1, 1)
g.SetPixel(0, 0, System.Drawing.Color.White)
MyBase.Image = g
ElseIf Value Is Nothing Then
Dim g As New System.Drawing.Bitmap(1, 1)
g.SetPixel(0, 0, System.Drawing.Color.White)
MyBase.Image = g
ElseIf Value.GetType.FullName = "System.Drawing.Bitmap" Then
MyBase.Image = Value
Dim ms As New System.IO.MemoryStream
CType(Value, Bitmap).Save(ms,
System.Drawing.Imaging.ImageFormat.Jpeg)
Dim bytBLOBData(ms.Length - 1) As Byte
ms.Position = 0
ms.Read(bytBLOBData, 0, ms.Length)
Value = bytBLOBData
Else
Dim img() As Byte = CType(Value, Byte())
Dim ms As New System.IO.MemoryStream
Dim offset As Integer = 0
ms.Write(img, offset, img.Length - offset)
Dim bmp As Bitmap = New Bitmap(ms)
ms.Close()
MyBase.Image = bmp
End If
mImage = Value
RaiseEvent ImageChanged(Me, New System.EventArgs)
End Set
End Property
End Class

-----------------End of Picturebox Control
-----------------Start of Image Pasting

Private Sub btnPasteImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPasteImage.Click
Dim data As IDataObject = Clipboard.GetDataObject
If (data.GetDataPresent(DataFormats.Bitmap)) Then
MyPictureBox1.Image = data.GetData(DataFormats.Bitmap)
End If
End Sub

-----------------End of Image Pasting


Nov 20 '05 #3

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

Similar topics

0
by: Stephen Williams | last post by:
Does Microsoft have a comment the possible bug on the PictureBox Class. Here is the exchange I had on the Experts-Exchange regarding this issue: From Z_Beeblebrox: Hi, I believe it is...
5
by: BrianW | last post by:
I am working on a program that has multiple picturebox controls that a user is allowed to move around which are contained within a panel control for visual placement. In my mousedown event, I set...
5
by: clickon | last post by:
This is driving me nuts, it is such a simply thing to do but i cannot for the life of me work out how you are suposed to do it. I want to update the data in DropDownListB based on what is...
4
by: munglet | last post by:
Is there anyway to get a picturebox to recieve focus? I ask because I implemented a "delete" button in a picturebox (due to size constraints), but now due to accessibility reasons I need to be...
1
by: Owen Blacker | last post by:
I've spent loads of time Googling to try to work this one out and I'm sure it's something obvious. I get an InvalidOperationException reading "Databinding methods such as Eval(), XPath(), and...
5
by: GoGs | last post by:
How this vb6 code convert in c# dotnet2 ----- Image1.Visible = False Image1.Picture = LoadPicture("c:\image008.jpg") Dim x, y As Single x = Image1.Width y = Image1.Height Do While x...
3
by: kirk | last post by:
I have a form with a PictureBox control on it. The .Image property is set to a PNG file(which shows the picture of the US map) with some transparency in it. The .BackColor property is set to...
5
by: AWW | last post by:
XP VB 2005 running an example from help that creates a picturebox in code - the picturebox is not created. If I comment out the "Dim Box as New PictureBox" and create it in Design mode - the...
1
by: leshka82 | last post by:
I have recently designed a Winform Image drag & drop utility. The approach I took was to have a Panel control serve as a destination for multiple file drop. Once the user dragged & dropped the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.