我想用pictureBox和panel来做
现在就是如果先缩小在放大会出现错误现象!
问题最终还是解决了,不过可能不是最优化,还请大家给看看,什么地方可能要做些改动.谢谢!!
private void btnBig_Click(object sender, EventArgs e)
{//放大图片
try
{
if (strName != "")
{
Bitmap MyBitmap = new Bitmap(this.pictureBox1.Image.Width*2, this.pictureBox1.Image.Height*2);
Graphics g = Graphics.FromImage(MyBitmap);
g.ScaleTransform(2, 2, System.Drawing.Drawing2D.MatrixOrder.Append);
g.DrawImage(this.pictureBox1.Image, 0, 0);
g.Save();
this.pictureBox1.Image = MyBitmap;
this.pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
this.panel1.AutoScroll = true;
this.panel1.Refresh();
this.pictureBox1.Refresh();
}
else
{
MessageBox.Show("请先选择图片", "信息提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
}
}
private void btnSmall_Click(object sender, EventArgs e)
{//缩小图片
try
{
if (strName != "")
{
Bitmap MyBitmap = new Bitmap(this.pictureBox1.Image.Width/2, this.pictureBox1.Image.Height/2);
Graphics g = Graphics.FromImage(MyBitmap);
g.ScaleTransform(0.5f, 0.5f, System.Drawing.Drawing2D.MatrixOrder.Append);
g.DrawImage(this.pictureBox1.Image, 0, 0);
g.Save();
this.pictureBox1.Image = MyBitmap;
this.panel1.AutoScroll = false;
this.panel1.Refresh();
this.pictureBox1.Refresh();
}
else
{
MessageBox.Show("请先选择图片","信息提示!",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
}
}
private void btnCir_Click(object sender, EventArgs e)
{//顺时针旋转
try
{
if (strName != "")
{
this.pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
int i = this.pictureBox1.Height;
this.pictureBox1.Height = this.pictureBox1.Width;
this.pictureBox1.Width = i;
this.pictureBox1.Refresh();
}
else
{
MessageBox.Show("请先选择图片", "信息提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{//逆时针旋转
try
{
if (strName != "")
{
this.pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipXY);
int i = this.pictureBox1.Height;
this.pictureBox1.Height = this.pictureBox1.Width;
this.pictureBox1.Width = i;
this.pictureBox1.Refresh();
}
else
{
MessageBox.Show("请先选择图片", "信息提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
}
}
private void btnZC_Click(object sender, EventArgs e)
{//正常显示
try
{
if (strName != "")
{
Bitmap MyBit = new Bitmap(strName);
Graphics g = Graphics.FromImage(MyBit);
g.DrawImage(MyBit, 0, 0);
g.Save();
this.pictureBox1.Image = MyBit;
this.pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
this.panel1.AutoScroll =true;
this.pictureBox1.Refresh();
}
else
{
MessageBox.Show("请先选择图片", "信息提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{//浏览图片
OpenFileDialog open = new OpenFileDialog();
open.Filter = "(*.jpg;*.bmp)|*.jpg;*.bmp|"+"(*.bmp)|*.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
strName = open.FileName;
Bitmap bt = new Bitmap(strName);
this.pictureBox1.Image = bt;
this.pictureBox1.Refresh();
}
}