110,536
社区成员
发帖
与我相关
我的任务
分享
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));
}
}
}
}
}
}
}
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