0

Getting this error when trying to create an image:

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

This is the code:

    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
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
David
  • 11
  • 2

1 Answers1

0

Thanks Plutonix and LarsTech

Changing it from Format16bppGrayScale to Format32bppPArgb and adding Using blocks the objects worked.

David
  • 11
  • 2