本人初学C#,此处有点不理解,还请大虾们指教!
下面是一个实现截图功能的代码,程序运行很正常。可就是截图功能在实现时,所显示的部分与所截取的部分分明不一致,求解释。谢了!(附上代码)using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace 截图
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Rectangle rect = new Rectangle(0, 0, 0, 0);
Point p1 = new Point(0, 0);
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "图片(*.jpg)|*.jpg";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Bitmap bmp = new Bitmap(openFileDialog1.FileName.ToString());
pictureBox1.Image = bmp;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
} } private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
rect.X = e.X;
rect.Y = e.Y;
} private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (e.X < rect.X)
p1.X = e.X;
else
p1.X = rect.X;
if (e.Y < rect.Y)
p1.Y = e.Y;
else
p1.Y = rect.Y;
panel1.Left = pictureBox1.Left + p1.X;
panel1.Top = pictureBox1.Top + p1.Y;
panel1.Width = Math.Abs(e.X - rect.X);
panel1.Height = Math.Abs(e.Y - rect.Y);
panel1.Visible = true;
}
else
panel1.Visible = false;
} private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
try
{
rect.X = p1.X;
rect.Y = p1.Y;
rect.Width = panel1.Width;
rect.Height = panel1.Height;
Bitmap bmp = new Bitmap(pictureBox1.Image);
pictureBox2.Image = bmp.Clone(rect, bmp.PixelFormat);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
下面是一个实现截图功能的代码,程序运行很正常。可就是截图功能在实现时,所显示的部分与所截取的部分分明不一致,求解释。谢了!(附上代码)using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace 截图
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Rectangle rect = new Rectangle(0, 0, 0, 0);
Point p1 = new Point(0, 0);
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "图片(*.jpg)|*.jpg";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Bitmap bmp = new Bitmap(openFileDialog1.FileName.ToString());
pictureBox1.Image = bmp;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
} } private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
rect.X = e.X;
rect.Y = e.Y;
} private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (e.X < rect.X)
p1.X = e.X;
else
p1.X = rect.X;
if (e.Y < rect.Y)
p1.Y = e.Y;
else
p1.Y = rect.Y;
panel1.Left = pictureBox1.Left + p1.X;
panel1.Top = pictureBox1.Top + p1.Y;
panel1.Width = Math.Abs(e.X - rect.X);
panel1.Height = Math.Abs(e.Y - rect.Y);
panel1.Visible = true;
}
else
panel1.Visible = false;
} private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
try
{
rect.X = p1.X;
rect.Y = p1.Y;
rect.Width = panel1.Width;
rect.Height = panel1.Height;
Bitmap bmp = new Bitmap(pictureBox1.Image);
pictureBox2.Image = bmp.Clone(rect, bmp.PixelFormat);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货