Expand|Select|Wrap|Line Numbers
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Drawing.Imaging;
- namespace load_image
- {
- /// <summary>
- /// Summary description for Form1.
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.OpenFileDialog openFileDialog1;
- private System.Windows.Forms.PictureBox pictureBox1;
- private System.Windows.Forms.PictureBox pictureBox2;
- private System.Windows.Forms.PictureBox pictureBox3;
- private Button button1;
- private Button button2;
- private IContainer components;
- 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.components = new System.ComponentModel.Container();
- System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
- this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
- this.pictureBox1 = new System.Windows.Forms.PictureBox();
- this.pictureBox2 = new System.Windows.Forms.PictureBox();
- this.pictureBox3 = new System.Windows.Forms.PictureBox();
- this.button1 = new System.Windows.Forms.Button();
- this.button2 = new System.Windows.Forms.Button();
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
- this.SuspendLayout();
- //
- // pictureBox1
- //
- this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
- this.pictureBox1.Location = new System.Drawing.Point(13, 3);
- this.pictureBox1.Name = "pictureBox1";
- this.pictureBox1.Size = new System.Drawing.Size(256, 240);
- this.pictureBox1.TabIndex = 0;
- this.pictureBox1.TabStop = false;
- //
- // pictureBox2
- //
- this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
- this.pictureBox2.Location = new System.Drawing.Point(284, 1);
- this.pictureBox2.Name = "pictureBox2";
- this.pictureBox2.Size = new System.Drawing.Size(256, 240);
- this.pictureBox2.TabIndex = 2;
- this.pictureBox2.TabStop = false;
- //
- // pictureBox3
- //
- this.pictureBox3.Location = new System.Drawing.Point(12, 256);
- this.pictureBox3.Name = "pictureBox3";
- this.pictureBox3.Size = new System.Drawing.Size(425, 231);
- this.pictureBox3.TabIndex = 3;
- this.pictureBox3.TabStop = false;
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(465, 429);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(75, 23);
- this.button1.TabIndex = 4;
- this.button1.Text = "Merge ";
- this.button1.UseVisualStyleBackColor = true;
- this.button1.Click += new System.EventHandler(this.button1_Click);
- //
- // button2
- //
- this.button2.Location = new System.Drawing.Point(465, 458);
- this.button2.Name = "button2";
- this.button2.Size = new System.Drawing.Size(75, 23);
- this.button2.TabIndex = 5;
- this.button2.Text = "Save";
- this.button2.UseVisualStyleBackColor = true;
- this.button2.Click += new System.EventHandler(this.button2_Click);
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
- this.ClientSize = new System.Drawing.Size(561, 497);
- this.Controls.Add(this.button2);
- this.Controls.Add(this.button1);
- this.Controls.Add(this.pictureBox3);
- this.Controls.Add(this.pictureBox2);
- this.Controls.Add(this.pictureBox1);
- this.Name = "Form1";
- this.Text = "Form1";
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- public Bitmap Merge( Bitmap bmp , Bitmap bmpimg)
- {
- BitmapData bmpData = bmpimg.LockBits( new Rectangle( 0 , 0 , bmpimg.Width , bmpimg.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );
- BitmapData bmpData2 = bmp.LockBits( new Rectangle( 0 , 0 , bmp.Width , bmp.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );
- int width = bmpData.Width;
- int height = bmpData.Height;
- if( bmpData2.Width > width )
- width = bmpData2.Width;
- if( bmpData2.Height > height )
- height = bmpData2.Height;
- bmpimg.UnlockBits( bmpData );
- bmp.UnlockBits( bmpData2 );
- Bitmap bit1 = new Bitmap( bmpimg , width , height );
- Bitmap bit2 = new Bitmap( bmp , width , height );
- Bitmap bmpresult = new Bitmap( width , height );
- BitmapData data1 = bit1.LockBits( new Rectangle( 0 , 0 , bit1.Width , bit1.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );
- BitmapData data2 = bit2.LockBits( new Rectangle( 0 , 0 , bit2.Width , bit2.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );
- BitmapData data3 = bmpresult.LockBits( new Rectangle( 0 , 0 , bmpresult.Width , bmpresult.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb);
- unsafe
- {
- int remain1 = data1.Stride - data1.Width * 3;
- int remain2 = data2.Stride - data2.Width * 3;
- int remain3 = data3.Stride - data3.Width * 3;
- byte* ptr1 = ( byte* )data1.Scan0;
- byte* ptr2 = ( byte* )data2.Scan0;
- byte* ptr3 = ( byte* )data3.Scan0;
- for( int i = 0 ; i < height ; i ++ )
- {
- for( int j = 0 ; j < width * 3 ; j ++ )
- {
- ptr3[ 0 ] = ( ptr2[ 0 ] > ptr1[ 0 ] ) ? ptr2[ 0 ] : ptr1[ 0 ];
- ptr1 ++;
- ptr2 ++;
- ptr3 ++;
- }
- ptr1 += remain1;
- ptr2 += remain2;
- ptr3 += remain3;
- }
- }
- bit1.UnlockBits( data1 );
- bit2.UnlockBits( data2 );
- bmpresult.UnlockBits( data3 );
- return bmpresult;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- pictureBox3.Image = Merge(new Bitmap(pictureBox2.Image), new Bitmap(pictureBox1.Image));
- }
- private void button2_Click(object sender, EventArgs e)
- {
- pictureBox3.Image.Save("C:\\mypic.jpg");
- }
- }
- }