在winform的窗体上浏览图片,要求可以移动、缩放,不知道怎么实现了!

@信心 2011-11-08 09:42:34
在winform作一l业务图像信息录入系统。左边体上浏览图像信息,右边是负责将图像里面信息录入。目前难点是左边的图像通过picturebox 展现出来 ,确定不可以移动、缩放,不知道怎么实现了!
...全文
173 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
@信心 2011-11-08
  • 打赏
  • 举报
回复
不好意思了!打字太快,上面有错别字,不要见怪!
@信心 2011-11-08
  • 打赏
  • 举报
回复
谢放贩楼上的代码,不过运行有点问题:
代码不完整,其中有两变量不什么代表什么
Pan_Main
Hsl_Scaling
allen0118 2011-11-08
  • 打赏
  • 举报
回复
参考:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace Test
{
public partial class TestForm : Form
{
public TestForm()
{
InitializeComponent();
}

private Image Src = null;

bool M_Down = false;
Point M_Location = Point.Empty;

private void Btn_Open_Click(object sender, EventArgs e)
{
if (this.Ofd_ImageFile.ShowDialog(this) == DialogResult.OK)
{
this.Txt_Path.Text = this.Ofd_ImageFile.FileName;
this.Pic_Image.Image = Src = Image.FromFile(this.Ofd_ImageFile.FileName);
}
}

private void TestForm_Load(object sender, EventArgs e)
{
this.MouseWheel += new MouseEventHandler(TestForm_MouseWheel);
}

void TestForm_MouseWheel(object sender, MouseEventArgs e)
{

Point mousePoint = this.PointToScreen(new Point(e.X, e.Y));

if (this.Pan_Main.RectangleToScreen(Pan_Main.DisplayRectangle).Contains(mousePoint))
{
//滚动
int value = this.Hsl_Scaling.Value + e.Delta / 120;
if (value <= this.Hsl_Scaling.Maximum && value >= this.Hsl_Scaling.Minimum)
{
this.Hsl_Scaling.Value = value;
}
}
}

private void Hsl_Scaling_ValueChanged(object sender, EventArgs e)
{
if (Src != null)
{
Size s = this.Pic_Image.Size;

this.Pic_Image.Size = new Size(Src.Width * this.Hsl_Scaling.Value, Src.Height * this.Hsl_Scaling.Value);
Bitmap bmp = new Bitmap(this.Pic_Image.Width, this.Pic_Image.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage(Src, 0, 0, bmp.Width, bmp.Height);
}
this.Pic_Image.Image = (Image)bmp.Clone();

if (s.Width > this.Pic_Image.Size.Width)
{
//缩小
this.Pic_Image.Location = new Point(this.Pic_Image.Left + (s.Width - this.Pic_Image.Size.Width) / 2, this.Pic_Image.Top + (s.Height - this.Pic_Image.Size.Height) / 2);
}
else if (s.Width < this.Pic_Image.Size.Width)
{
//放大
this.Pic_Image.Location = new Point(this.Pic_Image.Left - (this.Pic_Image.Size.Width - s.Width) / 2, this.Pic_Image.Top - (this.Pic_Image.Size.Height - s.Height) / 2);
}
}
}

private void Btn_Src_Click(object sender, EventArgs e)
{
if (this.Hsl_Scaling.Value == 1)
{
Hsl_Scaling_ValueChanged(sender, e);
}
else
{
this.Hsl_Scaling.Value = 1;
}
}

private void Pic_Image_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
M_Down = true;
this.M_Location = new Point(e.X, e.Y);
}
}

private void Pic_Image_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
M_Down = false;
}
}

private void Pic_Image_MouseMove(object sender, MouseEventArgs e)
{
if (M_Down && e.Button == MouseButtons.Left)
{
if (M_Down)
{
this.Pic_Image.Location = new Point(this.Pic_Image.Location.X + e.X - M_Location.X, this.Pic_Image.Location.Y + e.Y - M_Location.Y);
}
}
}
}
}



liukaizxc 2011-11-08
  • 打赏
  • 举报
回复
你把 左边的 当作一个子窗体 右边的当作父级 窗体 左边的就随便你放大和移动了呀

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧