1,453
社区成员
发帖
与我相关
我的任务
分享
//表示图片
private Image img;
//比率
private float ratio = 1F;
//图片被表示的范围
private Rectangle imgRect;
private void PicShow_Load(object sender, EventArgs e)
{
string path = @"E:\wxy\3.jpg";
img = Image.FromFile(path);
//this.Show(im);
imgRect = new Rectangle(0, 0, img.Width, img.Height);
ratio = 1F;
//表示图片
picBox.Invalidate();
}
//PictureBox1的MouseDown事件处理器
private void picBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
PictureBox pb = (PictureBox)sender;
//被点击的位置变换为图片上的位置
Point imgPoint = new Point(
(int)Math.Round((e.X - imgRect.X) / ratio),
(int)Math.Round((e.Y - imgRect.Y) / ratio));
//变更比率
if (e.Button == MouseButtons.Left)
{
ratio *= 2F;
}
else if (e.Button == MouseButtons.Right)
{
ratio *= 0.5F;
}
//计算图片的表示范围
imgRect.Width = (int)Math.Round(img.Width * ratio);
imgRect.Height = (int)Math.Round(img.Height * ratio);
imgRect.X = (int)Math.Round(pb.Width / 2 - imgPoint.X * ratio);
imgRect.Y = (int)Math.Round(pb.Height / 2 - imgPoint.Y * ratio);
//表示图片
picBox.Invalidate();
}
//PictureBox1的Paint事件处理器
private void picBox_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (img != null)
{
//在被指定的范围表示图片
e.Graphics.DrawImage(img, imgRect);
picBox.SizeMode = PictureBoxSizeMode.CenterImage;
}
}
'以下代码仅供参考
'将picture1中的图片缩放为picture2的宽度和高度后,保存为文件 D:\001.bmp
Option Explicit
'Form1上添加一个Command1,2个图片框picture1,picture2
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Declare Function SetStretchBltMode Lib "gdi32" (ByVal hdc As Long, ByVal nStretchMode As Long) As Long
Private Const HALFTONE = 4
Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
Private Sub Command1_Click()
Dim Rtn As Long
Dim hDC1 As Long, hDC2 As Long
hDC1 = Picture1.hdc
hDC2 = Picture2.hdc
Call SetStretchBltMode(hDC2, HALFTONE)
Rtn = StretchBlt(hDC2, 0, 0, Picture2.ScaleWidth, Picture2.ScaleHeight, hDC1, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, SRCCOPY)
SavePicture Picture2.Image, "D:\001.bmp"
End Sub
Private Sub Form_Load()
Me.ScaleMode = 3
Picture2.AutoRedraw = True
Picture1.Picture = LoadPicture("C:\TT.BMP")
End SubOption Explicit
Private m_oMyPic As IPictureDisp
Private Sub Form_Load()
Picture1.AutoRedraw = True
Set m_oMyPic = LoadPicture("c:\mypic.jpg")
Set Picture1.Picture = m_oMyPic
End Sub
Private Sub Form_Resize()
With Me
Picture1.Move 0, 0, .ScaleWidth, .ScaleHeight
End With
End Sub
Private Sub Picture1_Resize()
Picture1.PaintPicture m_oMyPic, 0, 0, Me.ScaleWidth, Me.ScaleHeight
End Sub