System.Drawing.Graphics.FromImage中的内存不足错误

时间:2014-09-03 16:48:58

标签: vb.net out-of-memory

尝试创建图片时出现此错误:

System.OutOfMemoryException: Out of memory.
 at System.Drawing.Graphics.FromImage(Image image)
 at eCom.MF.NewShippment(String LabelNumber, String OrderDate, String PackageType, String ServiceType, String PackageValue, Double Weight, Int32 Length, Int32 Width, Int32 Height, String FullName, String Address1, String Address2, String City, String State, String ZIP, String Country, String Phone, String Order) in C:\Users\youngdavidj\Documents\Visual Studio 2010\Projects\eCom\eCom\MainFreight.vb:line 183

这是代码:

    Dim FileName As String = My.Settings.ShippingLabelDir + LabelNumber + ".png"
    Try
        'Create Label Image for Main Frieght Shippment
        Dim label As Bitmap = New Bitmap(800, 1200, Imaging.PixelFormat.Format16bppGrayScale)
        Dim oImage As Graphics = Graphics.FromImage(label)
        Dim oBrushWrite As New SolidBrush(Color.Black)

        'Write text to image
        Dim text1 As String = "Main Freight"
        Dim Font1 As Font = New Font("Verdana", 30)
        Dim Point1 As PointF = New PointF(100, 20)
        oImage.DrawString(text1, Font1, oBrushWrite, Point1)

        Dim text2 As StringBuilder = New StringBuilder

        text2.Append("Ship From: " + My.Settings.ShipCompany + Environment.NewLine)
        text2.Append(My.Settings.ShipName + Environment.NewLine)
        text2.Append(My.Settings.ShipAddress1 + Environment.NewLine)
        text2.Append(My.Settings.ShipCity + ", " + My.Settings.ShipState + " " + My.Settings.ShipZip + Environment.NewLine)
        text2.Append(Environment.NewLine)
        text2.Append(Environment.NewLine)
        text2.Append("Ship To:" + Environment.NewLine)
        text2.Append(FullName + Environment.NewLine)
        text2.Append(Address1 + Environment.NewLine)
        If Len(Address2) > 0 Then
            text2.Append(Address2 + Environment.NewLine)
        End If
        text2.Append(City + ", " + State + " " + ZIP + Environment.NewLine)
        text2.Append(Phone)
        text2.Append(Environment.NewLine)
        text2.Append(Environment.NewLine)
        text2.Append("Order Date: " + OrderDate + Environment.NewLine)
        text2.Append("Label: " + LabelNumber + Environment.NewLine)
        text2.Append("Order: " + Order + Environment.NewLine)

        Dim Font2 As Font = New Font("Verdana", 12)
        Dim Point2 As PointF = New PointF(15, 100)
        oImage.DrawString(text2.ToString, Font2, oBrushWrite, Point2)

        'Save Image
        label.Save(FileName, Imaging.ImageFormat.Png)
        Debug.WriteLine("Main Freight Label Saved " + FileName)
        Debug.WriteLine("")

    Catch ex As Exception
        Debug.WriteLine("Error in drawing Main Freight label " + LabelNumber)
        Debug.WriteLine(ex.Message)
        Debug.WriteLine(ex.ToString)
        Debug.WriteLine("")
    End Try

1 个答案:

答案 0 :(得分:0)

感谢Plutonix和LarsTech

将其从Format16bppGrayScale更改为Format32bppPArgb并添加使用块来处理对象。