如何将变换尺寸后的图形文件保存为原文件?

sx_lxh 2005-05-27 11:46:33

程序为:
Dim mybitmap As Bitmap
mybitmap = New Bitmap("d:\a.jpg")'读取原文件
Dim flag As New System.Drawing.Bitmap(mybitmap, 100,100) '变换图片尺寸
PictureBox1.Image = flag ’将PictureBox1.Image为变换尺寸后的图片
Me.PictureBox2.Image.Save("d:\a.jpg") ’保存为原文件


运行时出错为:
未处理的“System.Runtime.InteropServices.ExternalException”类型的异常出现在 system.drawing.dll 中。其他信息: GDI+ 中发生一般性错误。



若保存为非原文件正常,请问如何才能保存为原文件?
...全文
81 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sx_lxh 2005-05-28
  • 打赏
  • 举报
回复
程序为:
Dim mybitmap As Bitmap
mybitmap = New Bitmap("d:\a.jpg")'读取原文件
Dim flag As New System.Drawing.Bitmap(mybitmap, 100,100) '变换图片尺寸
flag.Save("d:\a.jpg") ’保存为原文件


运行时出错为:
未处理的“System.Runtime.InteropServices.ExternalException”类型的异常出现在 system.drawing.dll 中。其他信息: GDI+ 中发生一般性错误。



若保存为非原文件正常,请问如何才能保存为原文件?
sx_lxh 2005-05-28
  • 打赏
  • 举报
回复
非常感谢“淡蓝色”!
淡蓝色2 2005-05-28
  • 打赏
  • 举报
回复
楼主的意思我大概明白了,不过你的代码过于简陋。
我给你写一段代码来实现你的目的:

'要处理的文件名
Dim strImageFileName As String = "D:\My Documents\My Pictures\复件 样品.jpg"
'源文件Image对象
Dim imgSource As Image
'目标文件Image对象
Dim bmpTarget As Bitmap
'变形后的尺寸(宽和高)
Dim iWidth, iHeight As Integer
'目标绘图对象
Dim g As Graphics

Try
iWidth = 100 : iHeight = 100
bmpTarget = New Bitmap(iWidth, iHeight, Drawing.Imaging.PixelFormat.Format24bppRgb)
g = Graphics.FromImage(bmpTarget)
imgSource = Image.FromFile(strImageFileName)
g.DrawImage(imgSource, New Rectangle(0, 0, iWidth, iHeight))
imgSource.Dispose() : imgSource = Nothing '释放源图像
bmpTarget.Save(strImageFileName, Imaging.ImageFormat.Jpeg)
Catch ex As Exception
'错误处理,暂不需要
Finally '释放绘图对象
If Not g Is Nothing Then g.Dispose()
If Not imgSource Is Nothing Then imgSource.Dispose()
If Not bmpTarget Is Nothing Then bmpTarget.Dispose()
End Try
sx_lxh 2005-05-27
  • 打赏
  • 举报
回复
Me.PictureBox2.Image.Save("d:\a.jpg") ’保存为原文件
输入有误,应为:
Me.PictureBox1.Image.Save("d:\a.jpg") ’保存为原文件

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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