add text to pic

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 フォーム デザイナで生成されたコード

        
/// <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();
        }

    }

}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值