关于pictruebox载入图片问题

rabemay 2008-04-28 08:56:50
怎样让载入的图片按pictruebox的大小缩放显示(不要让我用image)
...全文
148 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuzhicheng5 2009-11-05
  • 打赏
  • 举报
回复
       
//表示图片
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;
}
}
supergreenbean 2008-05-04
  • 打赏
  • 举报
回复
上面的都是VB6代码,只是为了看起来漂亮,所以用了代码格式化
rabemay 2008-05-03
  • 打赏
  • 举报
回复
我用的是vb 6.0
东方之珠 2008-04-29
  • 打赏
  • 举报
回复
'以下代码仅供参考
'将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 Sub
supergreenbean 2008-04-28
  • 打赏
  • 举报
回复
方便点,这样也可以:

Option 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
supergreenbean 2008-04-28
  • 打赏
  • 举报
回复
StretchBlt API

1,453

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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