using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.IO;using System.Drawing.Imaging;namespace StudySampleCSharp...{ /**//// <summary> /// Form1 の概要の説明です。 /// </summary> public class Form1 : System.Windows.Forms.Form ...{ private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox txtLogo; /**//// <summary> /// 必要なデザイナ変数です。 /// </summary> private System.ComponentModel.Container components = null; private System.Windows.Forms.Button btnForeColor; private System.Windows.Forms.Button btnBackColor; private System.Windows.Forms.Button btnFonts; private System.Windows.Forms.ComboBox cmbPosition; private System.Windows.Forms.Button btnExcute; private System.Windows.Forms.ListBox listBoxFile; private System.Windows.Forms.Button btnSource; private System.Windows.Forms.Button btnDestination; private System.Windows.Forms.TextBox txtDestination; private System.Windows.Forms.TextBox txtSource; private Font titleFont = new Font("MS UI Gothic",9); private Color foreColor = Color.White; private System.Windows.Forms.Label lblDemo; private System.Windows.Forms.PictureBox pictureBoxDemo; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private Color backColor = Color.Transparent; public Form1() ...{ // // Windows フォーム デザイナ サポートに必要です。 // InitializeComponent(); // // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。 // } /**//// <summary> /// 使用されているリソースに後処理を実行します。 /// </summary> protected override void Dispose( bool disposing ) ...{ if( disposing ) ...{ if (components != null) ...{ components.Dispose(); } } base.Dispose( disposing ); } Windows フォーム デザイナで生成されたコード#region Windows フォーム デザイナで生成されたコード /**//// <summary> /// デザイナ サポートに必要なメソッドです。このメソッドの内容を /// コード エディタで変更しないでください。 /// </summary> private void InitializeComponent() ...{ this.btnSource = new System.Windows.Forms.Button(); this.listBoxFile = new System.Windows.Forms.ListBox(); this.pictureBoxDemo = new System.Windows.Forms.PictureBox(); this.txtSource = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.txtDestination = new System.Windows.Forms.TextBox(); this.txtLogo = new System.Windows.Forms.TextBox(); this.btnFonts = new System.Windows.Forms.Button(); this.btnForeColor = new System.Windows.Forms.Button(); this.btnBackColor = new System.Windows.Forms.Button(); this.cmbPosition = new System.Windows.Forms.ComboBox(); this.btnExcute = new System.Windows.Forms.Button(); this.btnDestination = new System.Windows.Forms.Button(); this.lblDemo = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // btnSource // this.btnSource.Location = new System.Drawing.Point(512, 8); this.btnSource.Name = "btnSource"; this.btnSource.TabIndex = 1; this.btnSource.Text = "Source Fold"; this.btnSource.Click += new System.EventHandler(this.btnSource_Click); // // listBoxFile // this.listBoxFile.ItemHeight = 12; this.listBoxFile.Location = new System.Drawing.Point(8, 88); this.listBoxFile.Name = "listBoxFile"; this.listBoxFile.Size = new System.Drawing.Size(184, 304); this.listBoxFile.TabIndex = 3; this.listBoxFile.SelectedIndexChanged += new System.EventHandler(this.listBoxFile_SelectedIndexChanged); // // pictureBoxDemo // this.pictureBoxDemo.BackColor = System.Drawing.SystemColors.Info; this.pictureBoxDemo.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.pictureBoxDemo.Location = new System.Drawing.Point(384, 88); this.pictureBoxDemo.Name = "pictureBoxDemo"; this.pictureBoxDemo.Size = new System.Drawing.Size(304, 304); this.pictureBoxDemo.TabIndex = 4; this.pictureBoxDemo.TabStop = false; // // txtSource // this.txtSource.Location = new System.Drawing.Point(80, 8); this.txtSource.Name = "txtSource"; this.txtSource.Size = new System.Drawing.Size(408, 19); this.txtSource.TabIndex = 5; this.txtSource.Text = ""; // // label1 // this.label1.Location = new System.Drawing.Point(8, 8); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(56, 23); this.label1.TabIndex = 6; this.label1.Text = "Source:"; // // label2 // this.label2.Location = new System.Drawing.Point(8, 40); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(72, 23); this.label2.TabIndex = 7; this.label2.Text = "Destination:"; // // txtDestination // this.txtDestination.Location = new System.Drawing.Point(80, 40); this.txtDestination.Name = "txtDestination"; this.txtDestination.Size = new System.Drawing.Size(408, 19); this.txtDestination.TabIndex = 8; this.txtDestination.Text = ""; // // txtLogo // this.txtLogo.Location = new System.Drawing.Point(200, 96); this.txtLogo.Name = "txtLogo"; this.txtLogo.Size = new System.Drawing.Size(176, 19); this.txtLogo.TabIndex = 9; this.txtLogo.Text = ""; this.txtLogo.Validating += new System.ComponentModel.CancelEventHandler(this.txtLogo_Validating); // // btnFonts // this.btnFonts.Location = new System.Drawing.Point(200, 232); this.btnFonts.Name = "btnFonts"; this.btnFonts.TabIndex = 11; this.btnFonts.Text = "Fonts"; this.btnFonts.Click += new System.EventHandler(this.btnFonts_Click); // // btnForeColor // this.btnForeColor.Location = new System.Drawing.Point(200, 264); this.btnForeColor.Name = "btnForeColor"; this.btnForeColor.TabIndex = 12; this.btnForeColor.Text = "Front Color"; this.btnForeColor.Click += new System.EventHandler(this.btnForeColor_Click); // // btnBackColor // this.btnBackColor.Location = new System.Drawing.Point(200, 296); this.btnBackColor.Name = "btnBackColor"; this.btnBackColor.TabIndex = 13; this.btnBackColor.Text = "Back Color"; this.btnBackColor.Click += new System.EventHandler(this.btnBackColor_Click); // // cmbPosition // this.cmbPosition.Location = new System.Drawing.Point(200, 128); this.cmbPosition.Name = "cmbPosition"; this.cmbPosition.Size = new System.Drawing.Size(176, 20); this.cmbPosition.TabIndex = 14; this.cmbPosition.SelectedIndexChanged += new System.EventHandler(this.cmbPosition_SelectedIndexChanged); // // btnExcute // this.btnExcute.Location = new System.Drawing.Point(200, 344); this.btnExcute.Name = "btnExcute"; this.btnExcute.Size = new System.Drawing.Size(136, 23); this.btnExcute.TabIndex = 15; this.btnExcute.Text = "Excute"; this.btnExcute.Click += new System.EventHandler(this.btnExcute_Click); // // btnDestination // this.btnDestination.Location = new System.Drawing.Point(512, 40); this.btnDestination.Name = "btnDestination"; this.btnDestination.TabIndex = 16; this.btnDestination.Text = "Save Fold"; this.btnDestination.Click += new System.EventHandler(this.btnDestination_Click); // // lblDemo // this.lblDemo.BackColor = System.Drawing.SystemColors.Control; this.lblDemo.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lblDemo.Location = new System.Drawing.Point(496, 200); this.lblDemo.Name = "lblDemo"; this.lblDemo.Size = new System.Drawing.Size(96, 24); this.lblDemo.TabIndex = 17; this.lblDemo.Text = "label3"; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(264, 160); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 18; this.textBox1.Text = "textBox1"; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(264, 184); this.textBox2.Name = "textBox2"; this.textBox2.TabIndex = 19; this.textBox2.Text = "textBox2"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 12); this.ClientSize = new System.Drawing.Size(704, 422); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.lblDemo); this.Controls.Add(this.btnDestination); this.Controls.Add(this.btnExcute); this.Controls.Add(this.cmbPosition); this.Controls.Add(this.btnBackColor); this.Controls.Add(this.btnForeColor); this.Controls.Add(this.btnFonts); this.Controls.Add(this.txtLogo); this.Controls.Add(this.txtDestination); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.txtSource); this.Controls.Add(this.pictureBoxDemo); this.Controls.Add(this.listBoxFile); this.Controls.Add(this.btnSource); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /**//// <summary> /// アプリケーションのメイン エントリ ポイントです。 /// </summary> [STAThread] static void Main() ...{ Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) ...{ cmbPosition.Items.Add("TopLeft"); cmbPosition.Items.Add("TopRight"); cmbPosition.Items.Add("MiddleCenter"); cmbPosition.Items.Add("BottomLeft"); cmbPosition.Items.Add("BottomRight"); cmbPosition.SelectedIndex = 3; txtDestination.Text = @"C:Documents and Settings84デスクトップpic"; Demo(); } private void btnSource_Click(object sender, System.EventArgs e) ...{ string strFileName = ""; OpenFileDialog fDlg = new OpenFileDialog(); fDlg.Multiselect = true; if (fDlg.ShowDialog() != DialogResult.OK) ...{ return; } strFileName = fDlg.FileNames[0].ToString(); txtSource.Text = strFileName.Substring(0,strFileName.LastIndexOf("/")); for(int ix=0; ix < fDlg.FileNames.Length; ix++) ...{ strFileName = fDlg.FileNames[ix].ToString(); strFileName = strFileName.Substring(strFileName.LastIndexOf("/") + 1); listBoxFile.Items.Add(strFileName); } fDlg.Dispose(); } private void btnDestination_Click(object sender, System.EventArgs e) ...{ FolderBrowserDialog foldDlg = new FolderBrowserDialog(); if (foldDlg.ShowDialog() != DialogResult.OK) ...{ return; } txtDestination.Text = foldDlg.SelectedPath; } private void listBoxFile_SelectedIndexChanged(object sender, System.EventArgs e) ...{ string strFilePath; if (listBoxFile.SelectedIndex < 0) ...{ return; } strFilePath = txtSource.Text + "/" + listBoxFile.Items[listBoxFile.SelectedIndex]; if (File.Exists(strFilePath)) ...{ Image img = Image.FromFile(strFilePath); pictureBoxDemo.Image = img; img.Dispose(); } } private void btnFonts_Click(object sender, System.EventArgs e) ...{ FontDialog fontDlg = new FontDialog(); fontDlg.Font = titleFont; if (fontDlg.ShowDialog()!= DialogResult.OK) ...{ return; } titleFont = fontDlg.Font; fontDlg.Dispose(); Demo(); } private void btnForeColor_Click(object sender, System.EventArgs e) ...{ ColorDialog colorDlg = new ColorDialog(); colorDlg.Color = foreColor; if (colorDlg.ShowDialog()!= DialogResult.OK) ...{ return; } foreColor = colorDlg.Color; colorDlg.Dispose(); Demo(); } private void btnBackColor_Click(object sender, System.EventArgs e) ...{ ColorDialog colorDlg = new ColorDialog(); colorDlg.Color = backColor; if (colorDlg.ShowDialog()!= DialogResult.OK) ...{ return; } backColor = colorDlg.Color; colorDlg.Dispose(); Demo(); } private void btnExcute_Click(object sender, System.EventArgs e) ...{ string strLogoTitle = txtLogo.Text; if (strLogoTitle == string.Empty) ...{ MessageBox.Show("The LogoTitle can't be empty!"); txtLogo.Focus(); return; } if (listBoxFile.Items.Count < 1) ...{ MessageBox.Show("Please set the files that you want to add title to!"); return; } if (txtDestination.Text == string.Empty) ...{ MessageBox.Show("Please set the destination fold that you want to save the images!"); return; } string fileName = ""; string FILE_NAME = ""; System.Drawing.Image image; Bitmap bitmap; Graphics g; float rectX; float rectY; float rectWidth = strLogoTitle.Length*(titleFont.Size+8); float rectHeight = titleFont.Size+8; Brush frontBrush = new SolidBrush(foreColor); Brush backBrush = new SolidBrush(backColor); for (int ix = 0; ix < listBoxFile.Items.Count; ix++) ...{ fileName = txtSource.Text + "/"; fileName = fileName + listBoxFile.Items[ix].ToString(); if(!File.Exists(fileName)) ...{ continue; } image = System.Drawing.Image.FromFile(fileName); bitmap = new Bitmap(image,image.Width,image.Height); g = Graphics.FromImage(bitmap); switch(cmbPosition.SelectedIndex) ...{ case 0: rectX = 0; rectY = 0; break; case 1: rectX = image.Width - titleFont.Size * txtLogo.Text.Length; rectY = 0; break; case 2: rectX = image.Width / 2; rectY = image.Height / 2; break; case 3: rectX = 0; rectY = image.Height - titleFont.Size; break; case 4: rectX = image.Width - titleFont.Size * txtLogo.Text.Length; rectY = image.Height - titleFont.Size; break; default: rectX = 0; rectY = image.Height - titleFont.Size; break; } RectangleF textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight); g.FillRectangle(backBrush,rectX,rectY,rectWidth,rectHeight); g.DrawString(strLogoTitle,titleFont,frontBrush,textArea); FILE_NAME = txtDestination.Text + "/"; FILE_NAME = FILE_NAME + listBoxFile.Items[ix].ToString(); bitmap.Save(FILE_NAME,ImageFormat.Jpeg); g.Dispose(); bitmap.Dispose(); image.Dispose(); } MessageBox.Show("complete!"); } private void Demo() ...{ int iGood = 2; lblDemo.Text = txtLogo.Text; lblDemo.Width = 100; lblDemo.Height = 24; lblDemo.BackColor = backColor; lblDemo.ForeColor = foreColor; lblDemo.Font = titleFont; switch(cmbPosition.SelectedIndex) ...{ case 0: lblDemo.Left = pictureBoxDemo.Left + iGood; lblDemo.Top = pictureBoxDemo.Top + iGood; break; case 1: lblDemo.Left = pictureBoxDemo.Left + pictureBoxDemo.Width - lblDemo.Width - iGood; lblDemo.Top = pictureBoxDemo.Top + iGood; break; case 2: lblDemo.Left = pictureBoxDemo.Left + pictureBoxDemo.Width / 2 - lblDemo.Width / 2; lblDemo.Top = pictureBoxDemo.Top + pictureBoxDemo.Height / 2 - lblDemo.Height / 2; break; case 3: lblDemo.Left = pictureBoxDemo.Left + iGood; lblDemo.Top = pictureBoxDemo.Top + pictureBoxDemo.Height - lblDemo.Height - iGood; break; case 4: lblDemo.Left = pictureBoxDemo.Left + pictureBoxDemo.Width - lblDemo.Width - iGood; lblDemo.Top = pictureBoxDemo.Top + pictureBoxDemo.Height - lblDemo.Height - iGood; break; default: lblDemo.Left = pictureBoxDemo.Left + iGood; lblDemo.Top = pictureBoxDemo.Top + pictureBoxDemo.Height - lblDemo.Height - iGood; break; } } private void cmbPosition_SelectedIndexChanged(object sender, System.EventArgs e) ...{ Demo(); } private void txtLogo_Validating(object sender, System.ComponentModel.CancelEventArgs e) ...{ Demo(); } }}