用gdi+拼接多张大图(2000*11000像素),出错,求教。
vb吧
全部回复
仅看楼主
level 1

准备把10张jpg大图(每张2000*11000像素),纵向合并成一张jpg,最多只能合并5张,再多就出错。有时Bitmap3=0,有时Bitmap2=0,此时保存jpg文件就是0字节了。从任务管理器查看程序,内存占用500M以上,58%以上。核心代码如下:
(!!!考虑是内存的原因,太大。有没有办法,使用类似追加的功能,多次保存生成一个大文件???)
GdipCreateBitmapFromScan0 W, H, 0, GpPixelFormat.PixelFormat32bppARGB, ByVal 0, Bitmap3
GdipGetImageGraphicsContext Bitmap3, graphic预览
For i = 0 To 10
GdipLoadImageFromFile StrPtr(文件名), Bitmap2
If Bitmap2 = 0 Then Call 信息提示("读取图片错误:Bitmap2-0")
GdipDrawImageRect graphic预览, Bitmap2, 0, TT, 列表内容(i).W, 列表内容(i).H '拉伸到 '
TT = TT + 列表内容(i).H '生成下一个图片准备top数据
Next
Call GdipBitmapSetResolution(Bitmap3, 72, 72)
!!!考虑是内存的原因,太大。有没有办法,使用类似追加的功能,多次保存生成一个大文件???
以下是保存文件代码:
Public Function SaveImageToJPG(ByVal Image As Long, _
ByVal Path As String, _
ByVal quality As Long) As GpStatus
Dim Params As EncoderParameters
Params.Count = 1
CLSIDFromString StrPtr(EncoderQuality), Params.Parameter.GUID
Params.Parameter.NumberOfValues = 1
Params.Parameter.type = 4
Params.Parameter.Value = VarPtr(quality)
SaveImageToJPG = GdipSaveImageToFile(Image, StrPtr(Path), GetImageEncoderClsid(Jpg), Params)
2021年12月16日 10点12分 1
level 13
估计是内存限制。可以尝试一下当拼完一张图后,马上把拼完的图销毁(GdipDisposeImage),再拼接下一张。
2021年12月18日 01点12分 2
拼完的已经销毁了,就是这样做的
2021年12月21日 11点12分
level 11
如果你对jpg文件格式有研究的话,可以直接用二进制读写文件。
2021年12月18日 05点12分 3
没有研究
2021年12月21日 11点12分
level 14
我算了一下,2000 * 11000 的 ARGB 图约88兆,10张就是880兆拼接期间还需要一张临时的88兆,共968兆而已,也没多大啊。
2021年12月22日 02点12分 4
level 13
我试了一下,在VB6下,创建 11000*20000 的内存图像直接失败,在我的笔记本上只能创建 11000*6000的内存图像,也就是说只能拼接3个图像。代码如下:
Option Explicit
Private Sub Command1_Click()
On Error GoTo subErr
Dim bmp As Long
Dim g As Long
Dim i As Integer
Dim f As String
Dim y As Long
InitGDIPlus
GdipCreateBitmapFromScan0 11000, 20000, 0, PixelFormat24bppRGB, ByVal 0, bmp
GdipGetImageGraphicsContext bmp, g
For i = 1 To 10
Dim b As Long
f = "d:\" & i & ".jpg"
y = (i - 1) * 2000
GdipCreateBitmapFromFile StrPtr(f), b
If b = 0 Then
MsgBox "加载图像失败!" & fnvAS
Exit For
Else
GdipDrawImage g, b, 0, y
GdipDisposeImage b
End If
Next i
If i = 11 Then
If SaveImageToJPG(bmp, "d:\合并.jpg") Then
MsgBox "合并成功!"
Else
MsgBox "合并失败!"
End If
End If
If bmp <> 0 Then GdipDisposeImage bmp
If g <> 0 Then GdipDeleteGraphics g
TerminateGDIPlus
Exit Sub
subErr:
MsgBox Err.description
End Sub
又用VB.NET试了一下,可以拼接10个图像
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim b As Bitmap = New Bitmap(11000, 20000)
Dim g As Graphics = Graphics.FromImage(b)
Try
For i As Integer = 1 To 10
Dim b2 As Bitmap = Bitmap.FromFile("d:\" & i & ".jpg")
Dim y As Integer = (i - 1) * 2000
g.DrawImage(b2, 0, y)
b2.Dispose()
Next
b.Save("d:\22.jpg")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
2021年12月24日 03点12分 5
level 1
极限应用
2021年12月24日 13点12分 6
level 1
请问后面解决了吗?
2024年12月10日 00点12分 8
1