locked
A generic error occurred in GDI+. RRS feed

  • Question

  • User-1299682884 posted

    Hi Experts

    I am generating a barcode image from 6 digit numeric and my code is working fine through visual studio development web server but not working through local IIS web server.

    I receive error message "A generic error occurred in GDI+." at below code.  
    bmp.Save(xlocation, System.Drawing.Imaging.ImageFormat.Gif)

    The below is complete code:

        Private Sub getbcode()
    
            Dim Input As String = UCase(Request.QueryString("awb").ToString())
            'Dim input As String = UCase(TextBox1.Text.Trim())
            If Input = "" Then Input = "DRDIGIT.COM"
            Dim ValidInput As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"
            Dim ValidCodes As String = "4191459566472786097041902596264733841710595784729059950476626106644590602984801043246599624767444602600464775861090446866032248034439186013047842447705803036526582823575858090365863556658042365383495434978353624150635770"
            Dim i As Integer
            For i = 1 To Input.Length
                If InStr(1, ValidInput, Mid(Input, i, 1)) = 0 Then
                    Response.Write("Invalid input")
                    Response.End()
                End If
            Next
            Input = UCase(IIf(Left(Input, 1) = "*", "", "*") & Input & IIf(Right(Input, 1) = "*", "", "*"))
            Dim bmp As Bitmap = New Bitmap(Input.Length * 16, 50)
            Dim g As Graphics = Graphics.FromImage(bmp)
            g.FillRectangle(New SolidBrush(Color.White), 0, 0, Input.Length * 16, 50)
            Dim p As New Pen(Color.Black, 1)
            Dim BarValue, BarX As Integer
            Dim BarSlice As Short
            For i = 1 To Input.Length
                Try
                    BarValue = Val(Mid(ValidCodes, ((InStr(1, ValidInput, Mid(Input, i, 1)) - 1) * 5) + 1, 5))
                    BarValue = IIf(BarValue = 0, 36538, BarValue)
                    For BarSlice = 15 To 0 Step -1
                        If BarValue >= 2 ^ BarSlice Then
                            g.DrawLine(p, BarX, 0, BarX, 50)
                            BarValue = BarValue - (2 ^ BarSlice)
                        End If
                        BarX += 1
                    Next
                Catch
                End Try
            Next
    
            bmp.Save(xlocation, System.Drawing.Imaging.ImageFormat.Gif)
            imgcont.Src = "/Images/" & Request.QueryString("awb").ToString() & ".gif"
            lblno.Text = "&lt;p align='center'>" & Input & "</p>"
    
            g.Dispose()
            bmp.Dispose()
    
        End Sub
    

     Can someone please help me to resolve the above error message?

     Kind Regards

    Bhavesh

     

    Friday, February 27, 2009 3:14 PM

Answers

  • User1564875471 posted

    Hi,

    I receive error message "A generic error occurred in GDI+." at below code.  
    bmp.Save(xlocation, System.Drawing.Imaging.ImageFormat.Gif)

    It happeend to me before ,you application doesn't have write permission on the xlocation directory.

    Please make sure to add write permisison for network service account(or for aspnet account if your are running on XP) on that directory.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 27, 2009 3:21 PM

All replies

  • User1564875471 posted

    Hi,

    I receive error message "A generic error occurred in GDI+." at below code.  
    bmp.Save(xlocation, System.Drawing.Imaging.ImageFormat.Gif)

    It happeend to me before ,you application doesn't have write permission on the xlocation directory.

    Please make sure to add write permisison for network service account(or for aspnet account if your are running on XP) on that directory.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 27, 2009 3:21 PM
  • User-1299682884 posted

    Thanks.

     

     

    Monday, March 2, 2009 7:23 AM
  • User-1660525459 posted

     

    Hi, cud plz help me in solving my prob. I cannot crop my image & save to a folder. 

     

    protected void btnCrop_Click(object sender, EventArgs e)
            {
                string ImageName = Session["img"].ToString();
                int w = 40;
                int h = 60;
                int x = 50;
                int y = 70;

                byte[] CropImage = Crop(ImageName, w, h, x, y);
                using (MemoryStream ms = new MemoryStream(CropImage, 0, CropImage.Length))
                {
                    ms.Write(CropImage, 0, CropImage.Length);
                    using (SD.Image CroppedImage = SD.Image.FromStream(ms, true))
                    {
                        string SaveTo = ImageName + "CropImage" ;
                        CroppedImage.Save(SaveTo, CroppedImage.RawFormat);
                        pnlCrop.Visible = false;
                        pnlCropped.Visible = true;
                        imgCropped.ImageUrl = "Water lilies.jpg" + ImageName;
                    }
                }
            }

            static byte[] Crop(string Img, int Width, int Height, int X, int Y)
            {
                try
                {
                    using (SD.Image OriginalImage = SD.Image.FromFile(Img))
                    {
                        using (SD.Bitmap bmp = new SD.Bitmap(Width, Height, OriginalImage.PixelFormat))
                        {
                            bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
                            using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
                            {
                                Graphic.SmoothingMode = SmoothingMode.AntiAlias;
                                Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                                Graphic.DrawImage(OriginalImage, new SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel);
                                MemoryStream ms = new MemoryStream();
                               
                                bmp.Save(ms, OriginalImage.RawFormat);
                                return ms.GetBuffer();
                            }
                        }
                    }
                }
                catch (Exception Ex)
                {
                    throw (Ex);
                }
            }

    Wednesday, March 4, 2009 6:58 AM
  • User-1660525459 posted

    hay, i got the ans..its wrkin fine [:)]

    Wednesday, March 4, 2009 7:14 AM
  • User1810039979 posted

    I have a similar problem... I realized that the issue I was having was that I have wrote privileges to the location where I was saving a file but other users do not (and cannot.)

    Currently I have the page set up to draw the image in the codebehind, save it as a gif, then use the mapped path as the ImageUrl for an asp:Image control.  Can I use

    imagename.Save(Response.OutputStream, ImageFormat.Gif)
    -- instead of --
    imagename.Save(Server.MapPath(savefile), ImageFormat.Gif)

    and still control where the image appears within the page without setting up any frames or anything like that?  Thanks for your help.

    Here's the saving/displaying portion of my current code:

     

    Dim savefile As String = "compocompmap.gif"
    mapImage.Save(Server.MapPath(savefile), ImageFormat.Gif)
    Dim imagepath As String = savefile & "?dt=" & System.DateTime.Now
    compoCompImage.ImageUrl = imagepath
     
    Thursday, March 5, 2009 12:25 PM
  • User-1660525459 posted

    u can do it in both way, o/p obtained will b same

     CroppedImage.Save(Response.OutputStream, ImageFormat.Jpeg);

    Thursday, March 5, 2009 10:58 PM
  • User1810039979 posted

    My problem is placing the image within the page.  With the save and server.mappath function i can just create the image and save it to a location then display it where i have the asp.net image control.  With response.outputstream, it seems like it has to be a separate page that is accessed as the image.  Is there a way to use response.outputstream within the same page with all the other content?  Thanks.

    Friday, March 6, 2009 11:38 AM
  • User1564875471 posted

    With response.outputstream, it seems like it has to be a separate page that is accessed as the image.  Is there a way to use response.outputstream within the same page with all the other content?

    I don't think you can ! you need a seprate page/handler to render the image ,see the "The Image Grabber" section in this link:

    http://www.beansoftware.com/asp.net-tutorials/images-database.aspx

     

    Friday, March 6, 2009 11:53 AM