8,834
社区成员
发帖
与我相关
我的任务
分享
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 Test010
{
public partial class Form1 : Form
{
string PicPath = @"C:\VS2012Test\Test010\Test010\images\img18.jpg";
int RealWidth = 0;
int RealHeight = 0;
int ShowWidth = 0;
int ShowHeight = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Load(PicPath);
Image pic = Image.FromFile(PicPath);
RealWidth = pic.Width;
RealHeight = pic.Height;
ShowWidth = pictureBox1.Width;
ShowHeight = pictureBox1.Height;
}
public Image AcquireRectangleImage(Image source, Rectangle rect) //截取大图指定范围的小图
{
if (source == null || rect.IsEmpty) return null;
Bitmap bmSmall = new Bitmap(rect.Width, rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
using (Graphics grSmall = Graphics.FromImage(bmSmall))
{
grSmall.DrawImage(source, new System.Drawing.Rectangle(0, 0, bmSmall.Width, bmSmall.Height), rect, GraphicsUnit.Pixel);
grSmall.Dispose();
}
return bmSmall;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (RealWidth > 0)
{
using (var pict = new PictureBox())
{
int MouseX = e.X;
int MouseY = e.Y;
pict.Load(PicPath);
var im = AcquireRectangleImage(pict.Image, new Rectangle(MouseX * RealWidth / ShowWidth - pictureBox2.Width / 2, MouseY * RealHeight / ShowHeight - pictureBox2.Height / 2, pictureBox2.Width, pictureBox2.Height));
pictureBox2.Image = im;
label1.Text = MouseX.ToString();
label2.Text = MouseY.ToString();
label3.Text = (MouseX * RealWidth / ShowWidth - pictureBox2.Width / 2).ToString();
label4.Text = (MouseY * RealHeight / ShowHeight - pictureBox2.Height / 2).ToString();
}
}
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pictureBox2.Show();
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox2.Hide();
}
}
}