Databound PictureBox control

D

David Ricker

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
 
D

David Ricker

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
 
D

David Ricker

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top