如何将一张JPG图片,缩放成一定比例的图片子..请高手帮忙.
如何将一张JPG图片,缩放成一定比例的图片..请高手帮忙.
比如:123.JPG,长*宽=300*500,,我想生成宽为150的图片,长度按比例缩放.
生成的图尽量不失真,清楚,看上去与原图相似,但空间不能比原图占的还大...
来者有分,谢谢大家提供意见..
[解决办法]
BAIDU.COM上一大堆啊
[解决办法]
假设一个picture1 装入 你的jpg图象,将autosize 属性设置为 true .在放一个picture2,读出picture1的width 和height 的属性,然后根据你的需要进行缩放后设置成picture2的 width 和height 的属性。
然后
Picture2.PaintPicture picture1.image, 0, 0, Picture2.Width, Picture2.Height
然后再用savepicture 保存下来就可以了。
[解决办法]
哦,又學一招
[解决办法]
Option Explicit
Private Declare Function GetDC Lib "user32 " (ByVal hwnd As Long) As Long
Private Declare Function DeleteDC Lib "gdi32 " (ByVal hdc As Long) As Long
Private Declare Function ReleaseDC Lib "user32 " (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32 " (ByVal hdc As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32 " (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32 " (ByVal crColor As Long) As Long
Private Declare Function SelectObject Lib "gdi32 " (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32 " (ByVal hObject As Long) As Long
Private Declare Function FillRect Lib "user32 " (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
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 Declare Function OleCreatePictureIndirect Lib "olepro32.dll " (lpPictDesc As PICTDESC, riid As Any, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Private Const STRETCH_ANDSCANS = 1
Private Const STRETCH_ORSCANS = 2
Private Const STRETCH_DELETESCANS = 3
Private Const STRETCH_HALFTONE = 4
Private Type PICTDESC
Size As Long
Type As Long
hBmpOrIcon As Long
hPal As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
' 缩放图片
' 参数:源图片,新的高度,新的宽度,是否保持长宽比(默认为保持比例),保持长宽比时空白区域的填充颜色(默认为白色)
Public Function StretchPicture(ByVal Pic As StdPicture, ByVal NewWidth As Long, ByVal NewHeight As Long, Optional ByVal KeepScale As Boolean = True, Optional ByVal FillColor As OLE_COLOR = vbWhite) As StdPicture
Dim lngW As Long ' 原始图片宽度
Dim lngH As Long ' 原始图片高度
Dim lngNewW As Long ' 缩略图真实宽度
Dim lngNewH As Long ' 缩略图真实高度
Dim lngHdc1 As Long ' 内存DC1
Dim lngHdc2 As Long ' 内存DC2
Dim lngHdcScreen As Long ' 屏幕DC
Dim lngBmp As Long ' 新的位图句柄
Dim lngBmpOld1 As Long ' 原始位图句柄1
Dim lngBmpOld2 As Long ' 原始位图句柄2
Dim lngBrush As Long ' 填充画刷
Dim lngGuid(3) As Long
Dim sngDW As Single ' 宽度缩放比
Dim sngDH As Single ' 高度缩放比
Dim objPic As StdPicture
Dim udtRect As RECT
Dim udtPictDesc As PICTDESC
' 计算原始图片宽度和高度
lngW = Pic.Width * 1440 / 2540 / Screen.TwipsPerPixelX
lngH = Pic.Height * 1440 / 2540 / Screen.TwipsPerPixelX
' 创建两个兼容于屏幕的内存DC
lngHdcScreen = GetDC(0)
lngHdc1 = CreateCompatibleDC(lngHdcScreen)
lngHdc2 = CreateCompatibleDC(lngHdcScreen)
' 创建新的位图
lngBmp = CreateCompatibleBitmap(lngHdcScreen, NewWidth, NewHeight)
' 将位图加入到DC中
lngBmpOld1 = SelectObject(lngHdc1, Pic.handle)
lngBmpOld2 = SelectObject(lngHdc2, lngBmp)
lngNewW = NewWidth
lngNewH = NewHeight
If KeepScale Then
' 设置缩略图背景色
udtRect.Right = NewWidth
udtRect.Bottom = NewHeight
lngBrush = CreateSolidBrush(FillColor)
FillRect lngHdc2, udtRect, lngBrush
DeleteObject lngBrush
' 计算长宽缩放比
sngDW = NewWidth / lngW
sngDH = NewHeight / lngH
' 缩放比使用较小的一个,计算实际图片大小
If sngDW < sngDH Then
lngNewH = lngH * sngDW
Else
lngNewW = lngW * sngDH
End If
End If
' 设置缩放模式,该模式将影响处理速度和生成图片的质量
SetStretchBltMode lngHdc2, STRETCH_HALFTONE
StretchBlt lngHdc2, (NewWidth - lngNewW) / 2, (NewHeight - lngNewH) / 2, lngNewW, lngNewH, lngHdc1, 0, 0, lngW, lngH, vbSrcCopy
' 将位图转换成 StdPicture 对象
udtPictDesc.Size = Len(udtPictDesc)
udtPictDesc.Type = vbPicTypeBitmap
udtPictDesc.hBmpOrIcon = lngBmp
udtPictDesc.hPal = 0
lngGuid(0) = &H7BF80980
lngGuid(1) = &H101ABF32
lngGuid(2) = &HAA00BB8B
lngGuid(3) = &HAB0C3000
OleCreatePictureIndirect udtPictDesc, lngGuid(0), True, objPic
SelectObject lngHdc1, lngBmpOld1
SelectObject lngHdc2, lngBmpOld2
' 释放GDI对象
DeleteDC lngHdc1
DeleteDC lngHdc2
ReleaseDC 0, lngHdcScreen
Set StretchPicture = objPic
End Function
Private Sub Command1_Click()
Set Picture2.Picture = StretchPicture(Picture1.Picture, 512, 512, True, vbBlack)
End Sub
[解决办法]
//按比例缩小了一半后,生成的JPG反而是78K
你是如何生成的,要知道VB的 savepicture是按照BMP的结构保存的,即使你在保存时所使用的后缀名是.jpg
保存为JPG格式的图片是后好几个选项可以做的,如图片质量等。
[解决办法]
我的代码是缩放图片用的,用SavePicture方法只能保存成 BMP 格式的,可以把BMP格式再转换成JPG格式。
[解决办法]
如果生成文件反而大了,那就用jpg算法压缩下.看看。
[解决办法]
http://blog.csdn.net/online/archive/2005/07/13/424411.aspx
参考以下