Click here to Skip to main content
15,896,386 members
Articles / Web Development / HTML

XML Data Files, XML Serialization, and .NET

Rate me:
Please Sign up or sign in to vote.
4.82/5 (28 votes)
27 Aug 200317 min read 340.5K   6.4K   130  
Describes a means to build XML data files using XML Schema and xsd.exe to facilitate easy XML Serialization
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace CardfileSerializationDemo
{
	/// <summary>
	/// Summary description for frmImage.
	/// </summary>
	public class frmImage : frmCardEdit
	{
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button cmdBrowse;
		private System.Windows.Forms.PictureBox pbImage;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmImage()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			// Enable image drag and drop
			this.pbImage.AllowDrop = true;
		}

		/// <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.label3 = new System.Windows.Forms.Label();
			this.cmdBrowse = new System.Windows.Forms.Button();
			this.pbImage = new System.Windows.Forms.PictureBox();
			this.SuspendLayout();
			// 
			// cmdCancel
			// 
			this.cmdCancel.Visible = true;
			// 
			// cmdOK
			// 
			this.cmdOK.Visible = true;
			// 
			// tbCardName
			// 
			this.tbCardName.Visible = true;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 32);
			this.label3.Name = "label3";
			this.label3.TabIndex = 16;
			this.label3.Text = "&Image:";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// cmdBrowse
			// 
			this.cmdBrowse.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.cmdBrowse.Location = new System.Drawing.Point(216, 232);
			this.cmdBrowse.Name = "cmdBrowse";
			this.cmdBrowse.TabIndex = 19;
			this.cmdBrowse.Text = "&Browse";
			this.cmdBrowse.Click += new System.EventHandler(this.cmdBrowse_Click);
			// 
			// pbImage
			// 
			this.pbImage.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.pbImage.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.pbImage.Location = new System.Drawing.Point(8, 56);
			this.pbImage.Name = "pbImage";
			this.pbImage.Size = new System.Drawing.Size(440, 168);
			this.pbImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
			this.pbImage.TabIndex = 20;
			this.pbImage.TabStop = false;
			this.pbImage.DragEnter += new System.Windows.Forms.DragEventHandler(this.pbImage_DragEnter);
			this.pbImage.DragDrop += new System.Windows.Forms.DragEventHandler(this.pbImage_DragDrop);
			// 
			// frmImage
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(456, 261);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.cmdCancel,
																		  this.cmdOK,
																		  this.tbCardName,
																		  this.pbImage,
																		  this.cmdBrowse,
																		  this.label3});
			this.Name = "frmImage";
			this.Text = "New Image";
			this.ResumeLayout(false);

		}
		#endregion

		public Image Image
		{
			get { return pbImage.Image; }
			set { pbImage.Image = value; }
		}

		private void cmdBrowse_Click(object sender, System.EventArgs e)
		{
			// Load a card deck
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.AddExtension = true;
			dlg.CheckFileExists = true;
			dlg.CheckPathExists = true;
			dlg.DefaultExt = ".bmp";
			dlg.Filter = "Bitmap file (*.bmp)|*.bmp|Graphics Interchange Format (*.gif)|*.gif|JPEG file (*.jpg, *.jpeg)|*.bmp|Portable Network Graphics file (*.png)|*.png;*.jpeg|Image files (*.bmp,*.gif,*.jpg,*.jpeg,*.png)|*.bmp;*.gif;*.jpg;*.jpeg;*.png|All Files (*.*)|*.*";
			dlg.FilterIndex = 5; // "Image files"
			dlg.Multiselect = false;
			dlg.Title = "Open image file...";
			if ( dlg.ShowDialog() == DialogResult.OK )
			{
				// Set cursor
				this.Cursor = Cursors.WaitCursor;

				// Load the image file
				pbImage.Image = Bitmap.FromFile(dlg.FileName);

				// Reset cursor
				this.Cursor = Cursors.Arrow;
			} // if
		}

		private void pbImage_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
				e.Effect = DragDropEffects.Copy;
			else
				e.Effect = DragDropEffects.None;
		}

		private void pbImage_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
			{
				string[] filenames = (string[]) e.Data.GetData(DataFormats.FileDrop);
				if ( filenames.GetLength(0) > 0 )
				{
					try
					{
						// If we have at least one file, open the first
						// file...
						pbImage.Image = new Bitmap(filenames[0]);
					} // try
					catch (Exception ex)
					{
						string strErr = String.Format("Unable to load and display image, error '{0}'",ex.Message);
						MessageBox.Show(strErr,"Image Drop Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
					}
				} // if
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions