[Winform]截取鼠标附近的图片

wangchao1982 2009-04-24 05:56:53
标题:[Winform]截取鼠标附近的图片
描述:在一个pictureBox上,获取以鼠标所在位置为中心的图片矩形块(假如正方形的边长为2x).然后现实在另一个图片框里
...全文
79 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzuomin 2009-04-24
  • 打赏
  • 举报
回复
呵呵,代码随便改,只要您喜欢
lishuhua19820919 2009-04-24
  • 打赏
  • 举报
回复
楼上的我觉得改成,鼠标单击事件就更好了,楼主可以参考楼上代码修改下。
wzuomin 2009-04-24
  • 打赏
  • 举报
回复
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
Image source = null;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
source = new Bitmap(PictureBox1.Width, PictureBox1.Height);
OpenFileDialog ofd = new OpenFileDialog { Filter = "Image|*.jpg;*.bmp;*.png" };
if (ofd.ShowDialog() == DialogResult.OK)
{
using (Graphics g = Graphics.FromImage(source))
{
using (Image img = Image.FromFile(ofd.FileName))
{
g.DrawImage(img, new Rectangle(0, 0, source.Width, source.Height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
}
}
PictureBox1.Image = source;
}
else
{
this.Close();
}

}

private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (source != null)
{
if (e.X >= 10 && e.X <= PictureBox1.Width - 10)
{
if (e.Y >= 10 && e.Y <= PictureBox1.Height - 10)
{
using (Graphics g = PictureBox2.CreateGraphics())
{
g.DrawImage(source, new Rectangle(0, 0, PictureBox2.Width, PictureBox2.Height), new Rectangle(e.X - 10, e.Y - 10, 20, 20), GraphicsUnit.Pixel);
}
using (Graphics g = PictureBox1.CreateGraphics())
{
g.DrawImage(source, 0, 0);
g.DrawRectangle(Pens.White, new Rectangle(e.X - 10, e.Y - 10, 20, 20));
}
}
}
}

}
}
}
wzuomin 2009-04-24
  • 打赏
  • 举报
回复
Winform窗体放置PictureBox1、PictureBox2两个控件


Public Class Form1

Private source As Bitmap = Nothing

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
source = New Bitmap(PictureBox1.Width, PictureBox1.Height)
Dim ofd As New OpenFileDialog With {.Filter = "Image|*.jpg;*.bmp;*.png"}
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
Using g As Graphics = Graphics.FromImage(source), img As Image = Image.FromFile(ofd.FileName)
g.DrawImage(img, New Rectangle(0, 0, source.Width, source.Height), New Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel)
End Using
PictureBox1.Image = source
Else
Me.Close()
End If
End Sub

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If source IsNot Nothing Then
If e.X >= 10 AndAlso e.X <= PictureBox1.Width - 10 Then
If e.Y >= 10 AndAlso e.Y <= PictureBox1.Height - 10 Then
Using g As Graphics = PictureBox2.CreateGraphics
g.DrawImage(source, New Rectangle(0, 0, PictureBox2.Width, PictureBox2.Height), New Rectangle(e.X - 10, e.Y - 10, 20, 20), GraphicsUnit.Pixel)
End Using
Using g As Graphics = PictureBox1.CreateGraphics
g.DrawImage(source, 0, 0)
g.DrawRectangle(Pens.White, New Rectangle(e.X - 10, e.Y - 10, 20, 20))
End Using
End If
End If
End If
End Sub

End Class
格拉 2009-04-24
  • 打赏
  • 举报
回复
wangchao1982 2009-04-24
  • 打赏
  • 举报
回复
关键是我不知道从哪里下手
格拉 2009-04-24
  • 打赏
  • 举报
回复
应该不难吧

110,536

社区成员

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

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

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