|
发表于 2014-9-13 10:26:43
|
显示全部楼层
我这个是有5个小图片框,然后选中一个之后再将选中的图像加载到大图片框中,然后在大图片框汇总选择一块区域填充到刚才选中了小图片框中,至于你的事放大,则需要等图片等比例放大即可,下面是实例代码,您可以安装您的需要修改,这个是将图片打开到图片框
private void btnOpenImage_Click(object sender, EventArgs e)
{
//图像比原图缩小了40%,缩到60%的大小了
OpenFileDialog ofd = new OpenFileDialog();
ofd.AddExtension = true;
ofd.Multiselect = true;
ofd.DefaultExt = "jpg|bmp";
ofd.Filter = "bmp files (*.bmp)|*.bmp|jpg files (*.jpg)|*.jpg"; //|All files(*.*)|*.*
ofd.FilterIndex = 1;
int index = 0;
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
PictureBox tempPicb = null;
Label tempLabel = null;
CheckBox tempChk = null;
//Random rand = new Random();
//int n = rand.Next(1, 999);
if (ht.Count <= 0) //pb == null &&
{
for (int i = 0; i < ofd.FileNames.Length; i++)
{
tempPicb = SelectedPictureBox(i + 1);
tempLabel = GetCurrentLabel(i + 1);
tempChk = GetCurrentCheckBox(i + 1);
tempPicb.SizeMode = PictureBoxSizeMode.Zoom;
tempPicb.Image = Image.FromFile(ofd.FileNames[i]);
//n = rand.Next(1, 999) == n ? rand.Next(1, 999) : n;
//tempLabel.Text = n + "_" + ofd.SafeFileNames[i]; // + ofd.DefaultExt;
tempLabel.Text = ofd.SafeFileNames[i]; // + ofd.DefaultExt;
tempChk.Checked = true;
}
}
else if (ht.Count <= 1)
{
//这里只是修改一个或者替换一个图像
foreach (DictionaryEntry de in ht)
{
tempPicb = (PictureBox)de.Value;
tempLabel = GetCurrentLabel(Convert.ToInt32(de.Key));
tempChk = GetCurrentCheckBox(Convert.ToInt32(de.Key));
tempPicb.SizeMode = PictureBoxSizeMode.Zoom;
tempPicb.Image = Image.FromFile(ofd.FileNames[index]);
//n = rand.Next(1, 999) == n ? rand.Next(1, 999) : n;
//tempLabel.Text = n + "_" + ofd.SafeFileNames[index];
tempLabel.Text = ofd.SafeFileNames[index];
tempChk.Checked = true;
picbOriginalImage.SizeMode = PictureBoxSizeMode.Zoom;
picbOriginalImage.Image = tempPicb.Image;
lbImageName.Text = tempLabel.Text;
}
}
else //if (ht.Count > 1)
{
foreach (DictionaryEntry de in ht)
{
tempPicb = (PictureBox)de.Value;
tempLabel = GetCurrentLabel(Convert.ToInt32(de.Key));
tempChk = GetCurrentCheckBox(Convert.ToInt32(de.Key));
tempPicb.SizeMode = PictureBoxSizeMode.Zoom;
tempPicb.Image = Image.FromFile(ofd.FileNames[index]);
//n = rand.Next(1, 999) == n ? rand.Next(1, 999) : n;
//tempLabel.Text = n +"-" + ofd.SafeFileNames[index];
tempLabel.Text = ofd.SafeFileNames[index];
tempChk.Checked = true;
index++;
if (index >= ofd.FileNames.Length)
{
break;
}
}
}
//ht.Clear();
tempPicb = null;
}
}
图片框的选中
/// <summary>
/// 图像的点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PictureBox_Click(object sender, EventArgs e)
{
object tag = ((PictureBox)sender).Tag;
g_i_Flag = Convert.ToInt32(tag);
//Image image = null;
//pb = (PictureBox)sender; //将选中的对象给pb
PictureBox tempPb = (PictureBox)sender;
pb = tempPb;
if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
{
//MessageBox.Show("Ctrl键被按下");
//选中时改变样式
SelectedPictureBoxToStyle(g_i_Flag, true);
ht.Remove(tag);
ht.Add(tag, tempPb); //如果按下ctrl键选中那个添加那个
}
else
{
//移除所有之前选中的图像
ht.Clear();
ht.Add(tag, tempPb);
pictureBox1.BorderStyle = BorderStyle.FixedSingle;
pictureBox2.BorderStyle = BorderStyle.FixedSingle;
pictureBox3.BorderStyle = BorderStyle.FixedSingle;
pictureBox4.BorderStyle = BorderStyle.FixedSingle;
pictureBox5.BorderStyle = BorderStyle.FixedSingle;
//((PictureBox)sender).BorderStyle = BorderStyle.Fixed3D;
SelectedPictureBoxToStyle(g_i_Flag, true);
lb = GetCurrentLabel(g_i_Flag);
//image = pb.Image;
//pb = null;
if (tempPb.Image == null)
return;
//panel1.BackgroundImage = Image.FromFile(opf.FileName);
g_img_image = tempPb.Image;
picbOriginalImage.SizeMode = PictureBoxSizeMode.Zoom;
picbOriginalImage.Image = g_img_image;
lbImageName.Text = lb.Text;
srcImage = g_img_image;
srcWidth = srcImage.Width;
srcHeight = srcImage.Height;
thumbWidth = picbOriginalImage.Width;
thumbHeight = picbOriginalImage.Height;
}
tempPb = null;
}
鼠标按下事件
/// <summary>
/// 鼠标在图像上按下事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Image_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
//记录鼠标按下时的坐标位置
startPosX = e.X;
startPosY = e.Y;
isDraw = true;
//这里需要界定当鼠标按下的是左键时,鼠标区域控制,不能出picturebox框
//Point point = this.PointToScreen(new Point(19, 203)); //将窗体计算成屏幕坐标
Point point = this.PointToScreen(groupBox2.Location); //将窗体计算成屏幕坐标
point.X = point.X + picbOriginalImage.Location.X;
point.Y = point.Y + picbOriginalImage.Location.Y;
int offsetX = groupBox2.Location.X + picbOriginalImage.Location.X;
int offsetY = groupBox2.Location.Y + picbOriginalImage.Location.Y;
//Point point = new Point(100, 100);
//Point point = this.PointToClient(new Point(0, 0)); //将屏幕坐标计算成窗体
//this.ClientRectangle.Width, this.ClientRectangle.Height
Rectangle rect = new Rectangle(point, new Size(this.ClientSize.Width - offsetX - 125, this.ClientSize.Height - offsetY - 17));
Cursor.Clip = rect;
//MessageBox.Show(picbOriginalImage.Location.X + "," + picbOriginalImage.Location.Y + ";"
//+ groupBox2.Location.X + "," + groupBox2.Location.Y);
}
else
{
//说明按下的不是左键而是右键,这时需要重绘控件和子控件
Refresh();
}
}
鼠标在图像上抬起事件
/// <summary>
/// 鼠标在图像上抬起事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Image_MouseUp(object sender, MouseEventArgs e)
{
isDraw = false;
g.Dispose();
bool isReplaceImage = false;
#region
//将原图绘制成大小适中的小图
//Bitmap srcBmp = new Bitmap(srcWidth, srcHeight, PixelFormat.Format32bppArgb);
//Graphics srcG = Graphics.FromImage(srcBmp);
//srcG.InterpolationMode = InterpolationMode.HighQualityBicubic;
//srcG.SmoothingMode = SmoothingMode.AntiAlias;
//srcG.DrawImage(srcImage, Point.Empty);
//srcG.Flush(); //FlushIntention.Flush
////再选取小图中的区域
//Bitmap dstBmp = new Bitmap(dstWidth, dstHeight, PixelFormat.Format32bppArgb);
//Graphics dstG = Graphics.FromImage(dstBmp);
//dstG.DrawImage(srcBmp, 0, 0, new Rectangle(X, Y, dstWidth, dstHeight), GraphicsUnit.Pixel);
//dstG.Flush();
//dstG.Dispose();
//dstImage = dstBmp;
//pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
//pictureBox1.Image = dstImage;
#endregion
//这里需要界定,当鼠标抬起时解除鼠标区域控制
Screen[] srceens = Screen.AllScreens;
Cursor.Clip = srceens[0].Bounds;
if (e.Button == System.Windows.Forms.MouseButtons.Right)
return;
if (dstWidth <= 27 || dstHeight <= 20)
{
//MessageBox.Show("所选区域宽度不能小于27像素,高度不能小于20像素!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//如果pb==null的话说明没有选中picturebox
if (pb == null)
{
return;
}
else
{
//说明选中了其中的picturebox,在判断选中的picturebox有没有image
//if (pb.Image != null)
//{
// if (MessageBox.Show("确定要替换打印 " + g_i_Flag + " 的原图像吗?", "提示", MessageBoxButtons.OKCancel,
// MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
// {
// isReplaceImage = true;
// }
// else
// {
// isReplaceImage = false;
// }
//}
//else
//{
// isReplaceImage = true;
//}
isReplaceImage = true;
}
//说明需要替换
if (isReplaceImage)
{
DrawSelectedImage();
}
//pb = null;
}
鼠标在图片上移动事件
/// <summary>
/// 鼠标在图像上移动事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Image_MouseMove(object sender, MouseEventArgs e)
{
endPosX = e.X;
endPosY = e.Y;
if (isDraw)
{
Refresh();
//picbOriginalImage.Invalidate();
}
}
/// <summary>
/// 绘画
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Image_Paint(object sender, PaintEventArgs e)
{
//if (pb == null)
g = e.Graphics;
//g = picbOriginalImage.CreateGraphics();
//if (image == null)
// return;
//g = Graphics.FromImage(image);
//else
//g = pb.CreateGraphics();
if (isDraw)
{
//设置虚线格式
//Dash短线段,DashDot短线段和点,DashDotDot短线段点点,Dot点,Solid实线
pen.DashStyle = DashStyle.Dash;
//设置宽度和高度
dstWidth = endPosX - startPosX;
dstHeight = endPosY - startPosY;
//if (picbOriginalImage.Image == null)
// return;
//判断开始点结束点是从哪个方向到哪个方向
if (dstWidth < 0 && dstHeight < 0)
{
X = endPosX;
Y = endPosY;
dstWidth = Math.Abs(dstWidth);
dstHeight = Math.Abs(dstHeight);
}
else if (dstWidth < 0)
{
X = endPosX;
Y = startPosY;
dstWidth = Math.Abs(dstWidth);
}
else if (dstHeight < 0)
{
X = startPosX;
Y = endPosY;
dstHeight = Math.Abs(dstHeight);
}
else
{
X = startPosX;
Y = startPosY;
}
//目标矩形
Rectangle dstRectangle = new Rectangle(X, Y, dstWidth, dstHeight);
//这个是使用图像填充内部
//TextureBrush texture = new TextureBrush(srcImage);
//texture.WrapMode = WrapMode.Tile;
//画图像,这里需要填充到上面选中的框中
//g.DrawImageUnscaledAndClipped(image, dstRectangle);
//给矩形画填充内容
//g.DrawImage(srcImage, X, Y, dstRectangle, GraphicsUnit.Pixel);
//给矩形填充内容
//g.FillRectangle(texture, dstRectangle);
//画矩形框
g.DrawRectangle(pen, dstRectangle);
}
}
为刚才选区填充图像
/// <summary>
/// 为选中的区域填充图像
/// </summary>
private void DrawSelectedImage()
{
try
{
//将原图绘制成大小适中的小图
Bitmap thumbBmp = new Bitmap(thumbWidth, thumbHeight, PixelFormat.Format32bppArgb);
Graphics thumbG = Graphics.FromImage(thumbBmp);
thumbG.InterpolationMode = InterpolationMode.High;
thumbG.SmoothingMode = SmoothingMode.HighQuality;
thumbG.CompositingQuality = CompositingQuality.HighQuality;
Rectangle thumbRectangle = new Rectangle(0, 0, thumbWidth, thumbHeight);
thumbG.DrawImage(srcImage, thumbRectangle, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
thumbG.Flush(); //FlushIntention.Flush
thumbG.Dispose();
//再选取小图中的区域
Bitmap dstBmp = new Bitmap(dstWidth, dstHeight, PixelFormat.Format32bppArgb);
Graphics dstG = Graphics.FromImage(dstBmp);
Rectangle dstRectangle = new Rectangle(X, Y, dstWidth, dstHeight);
dstG.DrawImage(thumbBmp, 0, 0, dstRectangle, GraphicsUnit.Pixel);
dstG.Flush();
dstG.Dispose();
dstImage = dstBmp;
//ImageToSelectedPictureBox(g_i_Flag);
//pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
//pictureBox1.Image = dstImage;
//((PictureBox)((object)(pb.Name))).SizeMode = PictureBoxSizeMode.Zoom;
//((PictureBox)((object)(pb.Name))).Image = dstImage;
pb.SizeMode = PictureBoxSizeMode.Zoom;
pb.Image = dstImage;
//dstImage = null;
//thumbBmp.Dispose();
thumbBmp = null;
//dstBmp.Dispose();
dstBmp = null;
}
catch (Exception)
{
//
}
}
我这个最后返回的是image,您可以保存也可以做处理都可以 |
|