yeah respect icymint3, because i tried saving it using the file path but i am not sure if i did it right but it didn't work
yeah respect icymint3, because i tried saving it using the file path but i am not sure if i did it right but it didn't work
you are right, it doesnt allow you to bind to iamge from the dataset (because the types are different i guess).
Note -
m_IDentificationXMLDB is a strongly typed DataSet
m_IDentificationXMLDB.Identification is the identification DataTable
i just saved to an xml file because i didnt want to waste time creating an Access Database, and the data access code. but all you would do different is call yourIdentificationDataAdapter.Fill/Update.
i just typed the code - i bound to another field then changed the code in the generated section to "ImageData".
i then get the Binding object that corresponds to the Image property of the PictureBox, so i can handle the Parse and Format events.Code:Me.m_uiPbImage.DataBindings.Add(New System.Windows.Forms.Binding("Image", Me.m_IDentificationXMLDB, "Identification.ImageData"))
Implement the parse and Format handlers as followsCode:Private WithEvents m_ImageDataBinding As Binding Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.m_ImageDataBinding = Me.m_uiPbImage.DataBindings("Image") Try Me.m_uiPbImage.AllowDrop = True Me.m_IDentificationXMLDB.ReadXml(XMLFileName) Catch Me.m_IDentificationXMLDB.Identification.AddIdentificationRow("", Nothing, "", "", "", "") End Try End Sub
I see why you would have given up though, it took me much longer than i thought was necessary.Code:Private Sub m_ImageDataBinding_Format(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs) Handles m_ImageDataBinding.Format Try If e.DesiredType Is GetType(Image) Then Dim memstream As New System.IO.MemoryStream(CType(e.Value, Byte())) e.Value = Image.FromStream(memstream) memstream.Close() End If Catch ex As Exception e.Value = New System.Drawing.Bitmap(1, 1) End Try End Sub Private Sub m_ImageDataBinding_Parse(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs) Handles m_ImageDataBinding.Parse Try If e.DesiredType Is GetType(Byte()) Then Dim memstream As New System.IO.MemoryStream CType(e.Value, Image).Save(memstream, Imaging.ImageFormat.Jpeg) e.Value = memstream.ToArray memstream.Close() End If Catch ex As Exception e.Value = Nothing End Try End Sub
this is the important part of the code, i dont know how to get you the rest of the code if you want it :
if i commented the code then you'd have nothing to do, so i still think you should read the documentation on whatever you do not understand.Code:Private m_XMLFileName As String = "IdentificationDB.xml" Public Property XMLFileName() As String Get Try Return CType(Configuration.ConfigurationSettings.AppSettings("Form1.XMLFileName"), String) Catch Return Me.m_XMLFileName End Try End Get Set(ByVal Value As String) Me.m_XMLFileName = Value End Set End Property Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click Try Me.BindingContext(Me.m_IDentificationXMLDB, "Identification").EndCurrentEdit() Me.m_IDentificationXMLDB.WriteXml(XMLFileName) Catch x As Exception MsgBox(x.Message) End Try End Sub Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click Me.Close() End Sub Private Sub cmdNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNew.Click Me.BindingContext(Me.m_IDentificationXMLDB, "Identification").EndCurrentEdit() Me.m_IDentificationXMLDB.Identification.AddIdentificationRow("", Nothing, "", "", "", "") End Sub Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click Me.BindingContext(Me.m_IDentificationXMLDB, "Identification").EndCurrentEdit() CType(Me.BindingContext(Me.m_IDentificationXMLDB.Identification).Current, DataRowView).Delete() End Sub Private Sub m_uiPbImage_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_uiPbImage.DoubleClick Dim fd As New OpenFileDialog fd.CheckFileExists = True fd.CheckPathExists = True fd.Title = "Employee Identification Image" If fd.ShowDialog() <> DialogResult.OK Then Return Try Me.m_uiPbImage.Image = Image.FromFile(fd.FileName) Catch MessageBox.Show("Image could not be loaded", "Employee Identification Image Error") End Try End Sub
Last edited by icymint3; Jul 23, 2006 at 05:49 PM.
Cultured in Aggression and Koding like a Warrior!!
“Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
"The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein
is there a way to upload files with your posts?
xwire - crazy avatar!
Cultured in Aggression and Koding like a Warrior!!
“Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
"The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein
Excellent work icymint!
This one gave me a fight too, and I gave up after thinking it's not possibly.
Well, I did the binding VS2005 like easy, so then I tried the same approach in VS2002. After go through a lot more differences than I expected I finally reached the last important difference, but my attemtpt at binding failed.
The code was in C#
What I never think about was to do a Parse and Format handler for the Binding. Great work icymint.Code:this.pictureBox1.DataBindings.Add(new System.Windows.Forms.Binding("Image", this.dataSet11, "Categories.Picture"));
I would like a full code down load too if you are uploading.
This is my non binding implementation for VS2002
full code version in VS2005Code://Get all rows with ID above 3 from the table(_Categories) //within the dataset(categories) DataRow[] drArr = this.dataSet11.Categories.Select("CategoryID>3"); //Get a picture from row 1 & column 4, and store as a bytearray byte[] bArr = (byte[])(drArr[0][3]); //Load to memory stream System.IO.MemoryStream memStream = new System.IO.MemoryStream(bArr, 78, 10668); //Load Image Image img = System.Drawing.Bitmap.FromStream(memStream, true); this.pictureBox1.Image = img;
and full code in VS2002Code:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace PictureBindDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'categories._Categories' table. You can move, or remove it, as needed. this.categoriesTableAdapter.Fill(this.categories._Categories); //Get all rows with ID above 3 from the table(_Categories) //within the dataset(categories) DataRow[] drArr = this.categories._Categories.Select("CategoryID>3"); //Get a picture from row 1 & column 4, and store as a bytearray byte[] bArr = (byte[])(drArr[0][3]); //Load to memory stream System.IO.MemoryStream memStream = new System.IO.MemoryStream(bArr, 78, 10668); //Load Image Image img = System.Drawing.Bitmap.FromStream(memStream, true); this.pictureBox1.Image = img; try { this.categoriesTableAdapter.Insert("Vomitables", "Yuk", new byte[10746]); } catch(SystemException ex) { this.Text = "Picture Bind Demo - error - " + ex.Message; } try { this.categoriesTableAdapter.Update("Yumitables", "Yum", bArr, 9, "Vomitables"); } catch (SystemException ex) { this.Text = "Picture Bind Demo - error - " + ex.Message; } } private void pictureBox1_Click(object sender, EventArgs e) { //Get the currency manager for the binding used on the picture box CurrencyManager currMng = (CurrencyManager)(this.BindingContext[this.categoriesBindingSource]); //Use it to change the picture displayed if(currMng.Position == (currMng.Count - 1)) currMng.Position = 0; else currMng.Position++; } } }
Code:using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace PictureBind { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.PictureBox pictureBox1; private System.Data.OleDb.OleDbCommand oleDbSelectCommand1; private System.Data.OleDb.OleDbCommand oleDbInsertCommand1; private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1; private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1; private System.Data.OleDb.OleDbConnection oleDbConnection1; private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1; private System.Windows.Forms.TextBox textBox1; private PictureBind.DataSet1 dataSet11; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.dataSet11 = new PictureBind.DataSet1(); this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter(); this.textBox1 = new System.Windows.Forms.TextBox(); ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit(); this.SuspendLayout(); // // pictureBox1 // this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; //this.pictureBox1.DataBindings.Add(new System.Windows.Forms.Binding("Image", this.dataSet11, "Categories.Picture")); this.pictureBox1.Location = new System.Drawing.Point(16, 16); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(172, 120); this.pictureBox1.TabIndex = 1; this.pictureBox1.TabStop = false; this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click); // // dataSet11 // this.dataSet11.DataSetName = "DataSet1"; this.dataSet11.Locale = new System.Globalization.CultureInfo("en-JM"); this.dataSet11.Namespace = "http://www.tempuri.org/DataSet1.xsd"; // // oleDbSelectCommand1 // this.oleDbSelectCommand1.CommandText = "SELECT CategoryID, CategoryName, Description, Picture FROM Categories"; this.oleDbSelectCommand1.Connection = this.oleDbConnection1; // // oleDbConnection1 // this.oleDbConnection1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=J:\DBs\Office10\Samples\Northwind[edit].mdb;Mode=Share Deny None;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False"; // // oleDbInsertCommand1 // this.oleDbInsertCommand1.CommandText = "INSERT INTO Categories(CategoryName, Description, Picture) VALUES (?, ?, ?)"; this.oleDbInsertCommand1.Connection = this.oleDbConnection1; this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("CategoryName", System.Data.OleDb.OleDbType.VarWChar, 15, "CategoryName")); this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Description", System.Data.OleDb.OleDbType.VarWChar, 0, "Description")); this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Picture", System.Data.OleDb.OleDbType.VarBinary, 0, "Picture")); // // oleDbUpdateCommand1 // this.oleDbUpdateCommand1.CommandText = "UPDATE Categories SET CategoryName = ?, Description = ?, Picture = ? WHERE (Categ" + "oryID = ?) AND (CategoryName = ?) AND (Description = ? OR ? IS NULL AND Descript" + "ion IS NULL) AND (Picture = ? OR ? IS NULL AND Picture IS NULL)"; this.oleDbUpdateCommand1.Connection = this.oleDbConnection1; this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("CategoryName", System.Data.OleDb.OleDbType.VarWChar, 15, "CategoryName")); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Description", System.Data.OleDb.OleDbType.VarWChar, 0, "Description")); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Picture", System.Data.OleDb.OleDbType.VarBinary, 0, "Picture")); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_CategoryID", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(10)), ((System.Byte)(0)), "CategoryID", System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_CategoryName", System.Data.OleDb.OleDbType.VarWChar, 15,
Let's act on what we agree on now, and argue later on what we don't.
Black men leave Barbeque alone if Barbeque don't trouble you
Just saying that I tried too but my implementation is not as good as icymintsCode:System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryName", System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Description", System.Data.OleDb.OleDbType.VarWChar, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Description", System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Description1", System.Data.OleDb.OleDbType.VarWChar, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Description", System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Picture", System.Data.OleDb.OleDbType.VarBinary, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Picture", System.Data.DataRowVersion.Original, null)); this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Picture1", System.Data.OleDb.OleDbType.VarBinary, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Picture", System.Data.DataRowVersion.Original, null)); // // oleDbDeleteCommand1 // this.oleDbDeleteCommand1.CommandText = "DELETE FROM Categories WHERE (CategoryID = ?) AND (CategoryName = ?) AND (Descrip" + "tion = ? OR ? IS NULL AND Description IS NULL) AND (Picture = ? OR ? IS NULL AND" + " Picture IS NULL)"; this.oleDbDeleteCommand1.Connection = this.oleDbConnection1; this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_CategoryID", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(10)), ((System.Byte)(0)), "CategoryID", System.Data.DataRowVersion.Original, null)); this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_CategoryName", System.Data.OleDb.OleDbType.VarWChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryName", System.Data.DataRowVersion.Original, null)); this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Description", System.Data.OleDb.OleDbType.VarWChar, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Description", System.Data.DataRowVersion.Original, null)); this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Description1", System.Data.OleDb.OleDbType.VarWChar, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Description", System.Data.DataRowVersion.Original, null)); this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Picture", System.Data.OleDb.OleDbType.VarBinary, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Picture", System.Data.DataRowVersion.Original, null)); this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Picture1", System.Data.OleDb.OleDbType.VarBinary, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Picture", System.Data.DataRowVersion.Original, null)); // // oleDbDataAdapter1 // this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1; this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1; this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1; this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "Categories", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("CategoryID", "CategoryID"), new System.Data.Common.DataColumnMapping("CategoryName", "CategoryName"), new System.Data.Common.DataColumnMapping("Description", "Description"), new System.Data.Common.DataColumnMapping("Picture", "Picture")})}); this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1; // // textBox1 // this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "Categories.CategoryName")); this.textBox1.Location = new System.Drawing.Point(56, 152); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 2; this.textBox1.Text = ""; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(208, 189); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.textBox1, this.pictureBox1}); this.MaximizeBox = false; this.Name = "Form1"; this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide; this.Text = "Picture Bind Demo"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { this.oleDbDataAdapter1.Fill(this.dataSet11); //Get all rows with ID above 3 from the table(_Categories) //within the dataset(categories) DataRow[] drArr = this.dataSet11.Categories.Select("CategoryID>3"); //Get a picture from row 1 & column 4, and store as a bytearray byte[] bArr = (byte[])(drArr[0][3]); //Load to memory stream System.IO.MemoryStream memStream = new System.IO.MemoryStream(bArr, 78, 10668); //Load Image Image img = System.Drawing.Bitmap.FromStream(memStream, true); this.pictureBox1.Image = img; try { this.oleDbDataAdapter1.InsertCommand.Parameters["CategoryName"].Value = "cat"; this.oleDbDataAdapter1.InsertCommand.Parameters["Description"].Value = "des"; this.oleDbDataAdapter1.InsertCommand.Parameters["Picture"].Value = new byte [1024 * 16]; this.oleDbDataAdapter1.InsertCommand.Connection.Open(); this.oleDbDataAdapter1.InsertCommand.ExecuteNonQuery(); this.oleDbDataAdapter1.InsertCommand.Connection.Close(); } catch(System.Exception ex) { this.Text = "Picture Bind Demo - error - " + ex.Message; } } private void pictureBox1_Click(object sender, System.EventArgs e) { CurrencyManager currMng = (CurrencyManager)this.BindingContext[this.dataSet11, "Categories"]; if(currMng.Position == (currMng.Count - 1)) currMng.Position = 0; else currMng.Position++; } } }
Let's act on what we agree on now, and argue later on what we don't.
Black men leave Barbeque alone if Barbeque don't trouble you
I read something about the binding, dataset and format parameters earlier today. I thought picture binding could not be done because the code to do this was in the binding dll implementation. That was dumb because I read about building formats and using format parameters to retrieve data.
I never taught about getting under it like how icymint did .
I am reading up more on Format and Parse.
Let's act on what we agree on now, and argue later on what we don't.
Black men leave Barbeque alone if Barbeque don't trouble you
Edit :
Corrections. There is no format paramters.
This is itThe Binding class also enables you to format values for display through the Format event and to retrieve formatted values through the Parse event.
Let's act on what we agree on now, and argue later on what we don't.
Black men leave Barbeque alone if Barbeque don't trouble you
i will email u the code. is there an upload utility for techjamaica?
Cultured in Aggression and Koding like a Warrior!!
“Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
"The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein
Thanks, I will try to put it up on a site some how.
Let's act on what we agree on now, and argue later on what we don't.
Black men leave Barbeque alone if Barbeque don't trouble you
could you e-mail it to owayneb (i'm at work now and dont have access to the code), i didnt get to email it to him last night because my adsl a move a way (anxiously awaiting flow).
i will upload it later, but you guys have to download one file at a time because my free server does not allow upload of archives, it extracts then as soon as it uploads.
Last edited by icymint3; Jul 24, 2006 at 10:06 AM.
Cultured in Aggression and Koding like a Warrior!!
“Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
"The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein