vb中怎么改变图片大小并保存?

unimer 2002-10-03 11:58:42
我想做个旋转90度并保存,由于图片高宽要改变,用picturebox+api不知道怎么实现
或者其他简洁点的方法也行阿
...全文
1242 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
thirdapple 2002-10-03
  • 打赏
  • 举报
回复
看这段代码,旋转图片的:
http://www.csdn.net/expert/topic/900/900205.xml?temp=.9220545
'简易图象快速(不是最快)任意角度旋转DIB算法
'作者:刘留
'网名:Thirdapple
'E-Mail地址:3rdapple@sohu.com
'个人主页: http://3rdapple.51.net/
'通信地址:四川省遂宁市遂宁中学初2003级三班
'你可以任意传播此代码,但是请不要删除上面的说明文字,如果你对此代码进行了改进,请给我来信,谢谢!
Function CircumvolvingBits(FromPicture As PictureBox, ToPicture As PictureBox, Angle As Long, Zoom As Boolean)
Const Pi = 3.14159265358979 '定义的Pi值,好象是越多越好,于是就定义了这么多位:)
Dim x As Long, y As Long
Dim X1 As Long, Y1 As Long
Dim X2 As Double, Y2 As Double
Dim X3 As Long, Y3 As Long '这里原来定义的是Double,但是老出问题,定义为Long就正常了
Dim bit2Width As Long
Dim bitWidth As Long
Dim hOldMap As Long
Dim Pic2Bits() As Byte
Dim PicBits() As Byte
Dim iBitmap As Long, iDC As Long
Dim i2Bitmap As Long, i2DC As Long
Dim bi24BitInfo As BITMAPINFO
Dim bi24Bit2Info As BITMAPINFO
Dim HuDu As Single
Dim Pcolor As Long
'清除图片框ToPicture
ToPicture.Cls
'将角度转换为弧度
FromPicture.ScaleMode = vbPixels
ToPicture.ScaleMode = vbPixels
HuDu = Angle * Pi / 180
If Zoom = True Then '如果要图片框随图片的旋转而伸缩的话
If Angle < 90 Then '以下的代码都是运用三角函数进行的处理
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Width * Cos(HuDu) + FromPicture.Height * Sin(HuDu)
ToPicture.Height = FromPicture.Height * Cos(HuDu) + FromPicture.Width * Sin(HuDu)
If Angle = 0 Then
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Width
ToPicture.Height = FromPicture.Height
End If
Else
If Angle = 90 Then
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Height
ToPicture.Height = FromPicture.Width
End If
If Angle < 180 And Angle > 90 Then
Angle = Angle - 90
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Height * Cos(HuDu) + FromPicture.Width * Sin(HuDu)
ToPicture.Height = FromPicture.Width * Cos(HuDu) + FromPicture.Height * Sin(HuDu)
Else
If Angle = 180 Then
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Width
ToPicture.Height = FromPicture.Height
End If
If Angle < 270 And Angle > 180 Then
Angle = Angle - 180
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Width * Cos(HuDu) + FromPicture.Height * Sin(HuDu)
ToPicture.Height = FromPicture.Height * Cos(HuDu) + FromPicture.Width * Sin(HuDu)
Else
If Angle = 270 Then
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Height
ToPicture.Height = FromPicture.Width
End If
If Angle < 360 And Angle > 270 Then
Angle = Angle - 270
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Height * Cos(HuDu) + FromPicture.Width * Sin(HuDu)
ToPicture.Height = FromPicture.Width * Cos(HuDu) + FromPicture.Height * Sin(HuDu)
End If
End If
End If
End If
End If
With bi24BitInfo.bmiHeader '定义一个DIB位图结构
.biBitCount = 32 '定义为32位的DIB位图(很方便,以前我用24位的走了不少弯路),32位DIB位图每个象素有四个字节,分别是R、G、B、Alpha
.biCompression = BI_RGB
.biPlanes = 1
.biSize = Len(bi24BitInfo.bmiHeader)
.biWidth = FromPicture.ScaleWidth
.biHeight = FromPicture.ScaleHeight
.biSizeImage = .biWidth * 4 * .biHeight
End With
iDC = CreateCompatibleDC(0) '创建位图hDC
iBitmap = CreateDIBSection(iDC, bi24BitInfo, DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&) '创建位图
If iBitmap Then
hOldMap = SelectObject(iDC, iBitmap) '使i2DC与i2Bitmap建立关联
Else
DeleteObject iDC
Exit Function
End If
With bi24Bit2Info.bmiHeader '定义一个DIB位图结构
.biBitCount = 32 '定义为32位的DIB位图(很方便,以前我用24位的走了不少弯路),32位DIB位图每个象素有四个字节,分别是R、G、B、Alpha
.biCompression = BI_RGB
.biPlanes = 1
.biSize = Len(bi24BitInfo.bmiHeader)
.biWidth = ToPicture.ScaleWidth
.biHeight = ToPicture.ScaleHeight
.biSizeImage = .biWidth * 4 * .biHeight
End With
i2DC = CreateCompatibleDC(0) '创建位图hDC
i2Bitmap = CreateDIBSection(i2DC, bi24Bit2Info, DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&) '创建位图
If i2Bitmap Then
hOldMap = SelectObject(i2DC, i2Bitmap) '使i2DC与i2Bitmap建立关联
Else
DeleteObject i2DC
Exit Function
End If
BitBlt iDC, 0, 0, bi24BitInfo.bmiHeader.biWidth, bi24BitInfo.bmiHeader.biHeight, FromPicture.hDC, 0, 0, vbSrcCopy '将FromPicture上的图象拷贝到iDC中
bitWidth = bi24BitInfo.bmiHeader.biWidth * 4 '横向的字节总数
ReDim PicBits(0 To bitWidth * bi24BitInfo.bmiHeader.biHeight) As Byte '重新定义动态数组
GetBitmapBits iBitmap, bi24BitInfo.bmiHeader.biSizeImage, PicBits(0) '将iBitmap读取到PicBits数组中
BitBlt i2DC, 0, 0, bi24Bit2Info.bmiHeader.biWidth, bi24Bit2Info.bmiHeader.biHeight, ToPicture.hDC, 0, 0, vbSrcCopy '将ToPicture上的图象拷贝到i2DC中
bit2Width = bi24Bit2Info.bmiHeader.biWidth * 4 '横向的字节总数
ReDim Pic2Bits(0 To bit2Width * bi24Bit2Info.bmiHeader.biHeight) As Byte '重新定义动态数组
GetBitmapBits i2Bitmap, bi24Bit2Info.bmiHeader.biSizeImage, Pic2Bits(0) '将i2Bitmap读取到Pic2Bits数组中
'逐点旋转象素,并逐点复制
For x = 1 To ToPicture.ScaleWidth - 1
X1 = x - ToPicture.ScaleWidth \ 2
For y = 1 To ToPicture.ScaleHeight - 1
Y1 = y - ToPicture.ScaleHeight \ 2
'旋转象素点
X2 = X1 * Cos(-HuDu) + Y1 * Sin(-HuDu)
Y2 = Y1 * Cos(-HuDu) - X1 * Sin(-HuDu)
X3 = X2 + FromPicture.ScaleWidth \ 2
Y3 = Y2 + FromPicture.ScaleHeight \ 2
'如果象素点在待旋转位图内
If X3 >= 1 And X3 <= FromPicture.ScaleWidth - 1 Then
If Y3 >= 1 And Y3 <= FromPicture.ScaleHeight - 1 Then
'逐点复制位图
If x * 4 + 2 + y * bit2Width <= bit2Width * bi24Bit2Info.bmiHeader.biHeight _
And x * 4 + y * bit2Width >= 0 _
And X3 * 4 + 2 + Y3 * bitWidth <= bitWidth * bi24BitInfo.bmiHeader.biHeight _
And X3 * 4 + Y3 * bitWidth >= 0 Then
Pic2Bits(x * 4 + y * bit2Width) = PicBits(X3 * 4 + Y3 * bitWidth)
Pic2Bits(x * 4 + 1 + y * bit2Width) = PicBits(X3 * 4 + 1 + Y3 * bitWidth)
Pic2Bits(x * 4 + 2 + y * bit2Width) = PicBits(X3 * 4 + 2 + Y3 * bitWidth)
End If
End If
End If
Next y
Next x
SetBitmapBits i2Bitmap, bi24Bit2Info.bmiHeader.biSizeImage, Pic2Bits(0) '将Pic2Bits赋与i2Bitmap
BitBlt ToPicture.hDC, 0, 0, bi24Bit2Info.bmiHeader.biWidth, bi24Bit2Info.bmiHeader.biHeight, i2DC, 0, 0, vbSrcCopy '将i2DC拷贝到ToPicture中
'释放对象
If hOldMap Then DeleteObject SelectObject(iDC, hOldMap)
If hOldMap Then DeleteObject SelectObject(i2DC, hOldMap)
DeleteObject iDC '
DeleteObject i2DC
ToPicture.Refresh
CircumvolvingBits = True '返回真值
End Function
'还是要打上“原创”的记号(如是转载请打上“转载”的记号)
--------------------------------------------------------------------
如果Zoom设置为True的话,就可以根据图片旋转的角度自动改变图片框的大小,然后SavePicture就可以了
--------------------------------------------------------------------
欢迎使用Fantasia Photo(http://3rdapple.51.net/FantasiaPhoto.htm)
--------------------------------------------------------------------
Made by Thirdapple's Studio(http://3rdapple.51.net/)
mouseanAnya 2002-10-03
  • 打赏
  • 举报
回复
MARK + UP!

7,763

社区成员

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

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