2015-11-08 63 views
0

您好我有C#表單應用程序。我想將一幅圖像加載到一個圖片框中,並將圖像拖到我想要的位置。當我完成拖動後,我添加了一個複選框 ,讓用戶點擊複選框。在Picturebox中拖動和定位圖像並選擇圖像的一部分

然後,用戶可以使用鼠標來選擇圖像的一部分。該部分將顯示在另一個圖片框內。所以,我做了一些搜索,並提出了一個解決方案 ,這實際上不起作用。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace HCRLibrarytest 
{ 
    public partial class MainForm : Form 
    { 
     private Point startingPoint = Point.Empty; 
     private Point movingPoint = Point.Empty; 
     private bool panning = false; 
     Image _OrginalBitmap; 
     Image _NewBitmap; 
     private bool IsSelecting = false; 
     private int X0, Y0, X1, Y1; 
     static bool isimagepositioned = false; 

     public MainForm() 
     { 
      InitializeComponent(); 
     } 

     private void btn_openimage_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog dialog2 = new OpenFileDialog 
      { 
       Filter = "Bitmap Image (*.jpeg)|*.jpeg" 
      }; 

      using (OpenFileDialog dialog = dialog2) 
      { 
       if (dialog.ShowDialog() == DialogResult.OK) 
       { 
        try 
        { 
         using (StreamReader reader = new StreamReader(dialog.FileName)) 
         { 
          this._OrginalBitmap = new Bitmap(dialog.FileName); 
          this.pb_fullimage.Image = this._OrginalBitmap; 
         } 
        } 
        catch (Exception exception) 
        { 
         MessageBox.Show(exception.ToString()); 
        } 
       } 
      } 
     } 

     private void pb_fullimage_MouseUp(object sender, MouseEventArgs e) 
     { 
      if (_OrginalBitmap != null) 
      { 
       if(!isimagepositioned) 
       { 
        panning = false; 
       } 

       else 
       { 
        if (!IsSelecting) return; 
        IsSelecting = false; 
        pb_fullimage.Image = _OrginalBitmap; 
        int wid = Math.Abs(X0 - X1); 
        int hgt = Math.Abs(Y0 - Y1); 
        if ((wid < 1) || (hgt < 1)) return; 
        Bitmap area = new Bitmap(wid, hgt); 
        using (Graphics gr = Graphics.FromImage(area)) 
        { 
         Rectangle source_rectangle = new Rectangle(Math.Min(X0, X1), Math.Min(Y0, Y1), wid, hgt); 
         Rectangle dest_rectangle = new Rectangle(0, 0, wid, hgt); 
         gr.DrawImage(pb_fullimage.Image, dest_rectangle, source_rectangle, GraphicsUnit.Pixel); 
        } 
        pb_selectedportion.Image = area; 
       } 
      } 
     } 

     private void pb_fullimage_MouseDown(object sender, MouseEventArgs e) 
     { 
      if (_OrginalBitmap != null) 
      { 
       if (!isimagepositioned) 
       { 
        panning = true; 
        startingPoint = new Point(e.Location.X - movingPoint.X,e.Location.Y - movingPoint.Y); 
       } 
       else 
       { 
        _NewBitmap = new Bitmap(pb_fullimage.Image); 
        IsSelecting = true; 
        X0 = e.X; 
        Y0 = e.Y; 
       } 
      } 
     } 

     private void pb_fullimage_MouseMove(object sender, MouseEventArgs e) 
     { 
      if (_OrginalBitmap != null) 
      { 
       if (!isimagepositioned) 
       { 
        if (panning) 
        { 
         movingPoint = new Point(e.Location.X - startingPoint.X,e.Location.Y - startingPoint.Y); 
         pb_fullimage.Invalidate(); 
        } 
       } 

       else 
       { 
        if (!IsSelecting) return; 
        X1 = e.X; 
        Y1 = e.Y; 
        Bitmap bm = new Bitmap(_NewBitmap); 
        using (Graphics gr = Graphics.FromImage(bm)) 
        { 
         gr.DrawRectangle(Pens.Red, Math.Min(X0, X1), Math.Min(Y0, Y1), Math.Abs(X0 - X1), Math.Abs(Y0 - Y1)); 
        } 
        pb_fullimage.Image = bm; 
       } 
      } 
     } 

     private void pb_fullimage_Paint(object sender, PaintEventArgs e) 
     { 
      if (_OrginalBitmap != null && !isimagepositioned) 
      { 
       e.Graphics.Clear(Color.White); 
       e.Graphics.DrawImage(_OrginalBitmap, movingPoint); 
      } 
     } 

     private void checkBox1_CheckedChanged(object sender, EventArgs e) 
     { 
      if (checkBox1.Checked) 
      { 
       isimagepositioned = true; 
      } 
      else 
      { 
       isimagepositioned = false; 
      } 
     } 
    } 
} 

當我拖動並選中「圖像定位」並選擇使用鼠標移動。它總是給我與原始圖像位置相關的圖像。

那麼,有人可以幫助解決這個問題。

enter image description here

回答

1

由於沒有人回答我,我找到了答案。這對我有效。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace HCRLibrarytest 
{ 
    public partial class MainForm : Form 
    { 
     public Point startingPoint = Point.Empty; 
     public Point movingPoint = Point.Empty; 
     public bool panning = false; 
     Image _OrginalBitmap; 
     public static Image _NewBitmap; 
     public bool IsSelecting = false; 
     public int X0, Y0, X1, Y1; 

     public MainForm() 
     { 
      InitializeComponent(); 
     } 

     public void btn_openimage_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog dialog2 = new OpenFileDialog 
      { 
       Filter = "Bitmap Image (*.jpeg)|*.jpeg" 
      }; 

      using (OpenFileDialog dialog = dialog2) 
      { 
       if (dialog.ShowDialog() == DialogResult.OK) 
       { 
        try 
        { 
         using (StreamReader reader = new StreamReader(dialog.FileName)) 
         { 
          this._OrginalBitmap = new Bitmap(dialog.FileName); 
          this.pb_fullimage.Image = this._OrginalBitmap; 
         } 
        } 
        catch (Exception exception) 
        { 
         MessageBox.Show(exception.ToString()); 
        } 
       } 
      } 
     } 

     public void pb_fullimage_MouseUp(object sender, MouseEventArgs e) 
     { 
      if (pb_fullimage.Image != null) 
      { 
       if (!checkBox1.Checked) 
       { 
        panning = false; 
       } 

       else 
       { 
        if (!IsSelecting) return; 
        IsSelecting = false; 
        pb_fullimage.Image = _NewBitmap; 
        int wid = Math.Abs(X0 - X1); 
        int hgt = Math.Abs(Y0 - Y1); 
        if ((wid < 1) || (hgt < 1)) return; 
        Bitmap area = new Bitmap(wid, hgt); 
        using (Graphics gr = Graphics.FromImage(area)) 
        { 
         Rectangle source_rectangle = new Rectangle(Math.Min(X0, X1), Math.Min(Y0, Y1), wid, hgt); 
         Rectangle dest_rectangle = new Rectangle(0, 0, wid, hgt); 
         gr.DrawImage(_NewBitmap, dest_rectangle, source_rectangle, GraphicsUnit.Pixel); 
        } 
        pb_selectedportion.Image = area; 
       } 
      } 
     } 

     public void pb_fullimage_MouseDown(object sender, MouseEventArgs e) 
     { 
      if (pb_fullimage.Image != null) 
      { 
       if (!checkBox1.Checked) 
       { 
        panning = true; 
        startingPoint = new Point(e.Location.X - movingPoint.X, e.Location.Y - movingPoint.Y); 
       } 
       else 
       { 
        IsSelecting = true; 
        X0 = e.X; 
        Y0 = e.Y; 
       } 
      } 
     } 

     public void pb_fullimage_MouseMove(object sender, MouseEventArgs e) 
     { 
      if (pb_fullimage.Image != null) 
      { 
       if (!checkBox1.Checked) 
       { 
        if (panning) 
        { 
         movingPoint = new Point(e.Location.X - startingPoint.X, e.Location.Y - startingPoint.Y); 
         pb_fullimage.Invalidate(); 
         using (Bitmap bitmap = new Bitmap(pb_fullimage.ClientSize.Width, pb_fullimage.ClientSize.Height)) 
         { 
          pb_fullimage.DrawToBitmap(bitmap, pb_fullimage.ClientRectangle); 
          try { bitmap.Save(AppDomain.CurrentDomain.BaseDirectory + "draw.jpg"); } 
          catch (Exception ex) { Console.Write(ex.ToString()); } 
         } 
        } 
       } 

       else 
       { 
        if (!IsSelecting) return; 
        X1 = e.X; 
        Y1 = e.Y; 
        Bitmap bm = new Bitmap(Bitmap.FromStream(File.OpenRead(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "draw.jpg")))); 
        using (Graphics gr = Graphics.FromImage(bm)) 
        { 
         gr.DrawRectangle(Pens.WhiteSmoke, Math.Min(X0, X1), Math.Min(Y0, Y1), Math.Abs(X0 - X1), Math.Abs(Y0 - Y1)); 
        } 
        _NewBitmap = bm; 
       } 
      } 
     } 

     public void pb_fullimage_Paint(object sender, PaintEventArgs e) 
     { 
      if (pb_fullimage.Image != null && !checkBox1.Checked) 
      { 
       e.Graphics.Clear(Color.White); 
       e.Graphics.DrawImage(pb_fullimage.Image, movingPoint); 
      } 
     } 
    } 
}