Clip Text&Image

Clipboard に文字列とイメージを一緒に格納します。

前田稔(Maeda Minoru)の超初心者のプログラム入門

文字列と画像を格納

  1. Clipboard に文字列と画像を格納する ClipTxtImgSet.cs の全ソースコードです。
    /******************************************************/
    /*★ クリップボードに文字列と画像を格納     前田 稔 ★*/
    /******************************************************/
    using System;
    using System.Drawing;
    using System.Drawing.Text;
    using System.Windows.Forms;
    
    public class SetClipboard
    {
        [STAThread]
        public static int Main()
        {
            DataObject data = new DataObject();
            string text = "こんにちは C#";
            data.SetData(text);
            Bitmap bmp = new Bitmap(320, 100);
            Graphics g = Graphics.FromImage(bmp);
            g.FillRectangle(new SolidBrush(Color.DarkGreen), 0, 0, 200, 60);
            g.DrawString(text, new Font("Times New Roman", 20), new SolidBrush(Color.Gold), 20, 10);
            data.SetData(bmp);
            Clipboard.SetDataObject(data, true);
            return 0;
        }
    }
    
  2. 文字列とイメージを一緒に格納するときは DataObject を直接定義します。
    data.SetData(text) で文字列を格納しておいて data.SetData(bmp) で画像を追加します。
    そして SetDataObject(data, true); でクリップボードに格納します。

文字列と画像を取得

  1. ClipTxtImgSet で格納した文字列と画像を取得して描画する ClipTxtImgGet.cs の全ソースコードです。
    Form にボタンと TextBox と PictureBox を配置しています。
    /************************************************************/
    /*★ クリップボードの Text と Image を表示する    前田 稔 ★*/
    /************************************************************/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MyForm : Form
    {
        private TextBox textBox1;
        private PictureBox pictureBox1;
        private Button button1;
    
        public MyForm()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // button1
            this.button1.Location = new System.Drawing.Point(3, 3);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(100, 22);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // textBox1
            this.textBox1.Location = new System.Drawing.Point(3, 31);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(200, 25);
            this.textBox1.TabIndex = 1;
            // pictureBox1
            this.pictureBox1.Location = new System.Drawing.Point(3, 62);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(200, 60);
            this.pictureBox1.TabIndex = 2;
            this.pictureBox1.TabStop = false;
            // MyForm
            this.ClientSize = new System.Drawing.Size(220, 140);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Name = "MyForm";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
           textBox1.Text = "";
           pictureBox1.Image = null;
           IDataObject data = Clipboard.GetDataObject();
           if (data.GetDataPresent(DataFormats.Text))
           {   textBox1.Text = (string)data.GetData(DataFormats.Text);
           }
           if (data.GetDataPresent(DataFormats.Bitmap))
           {   pictureBox1.Image = (Image)data.GetData(DataFormats.Bitmap);
           }
        }
    }
    
    class form01
    {
        [STAThread]
        public static void Main()
        {
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    
  2. ボタンをクリックするとクリップボードから文字列と画像を取得して描画します。
    事前に ClipTxtImgSet を実行して文字列と画像を格納しておいて下さい。
    if (data.GetDataPresent(DataFormats.Text)) でクリップボードに文字列が格納されているかを調べます。
    格納されているときは TextBox に表示します。
    if (data.GetDataPresent(DataFormats.Bitmap)) でクリップボードに画像が格納されているかを調べます。
    格納されているときは PictureBox に表示します。

超初心者のプログラム入門(C# Frame Work)