System.Drawing: Error when using a .bmp or .gif file but not when .jpg

Discussion in 'ASP .Net' started by Richard, Jun 15, 2004.

  1. Richard

    Richard Guest

    I each time get errors when I use an .bmp or ,.gif file as source but
    whenever I use a .jpg file it works perfect.
    Why is that? and how to fix it?

    I almost have no knowledge about the system.drawing and so I am in the dark
    about this.



    This is my code
    ----------------------------------

    <%@ Page Language="vb" %>
    <%@ import namespace="system.drawing" %>
    <%@ import namespace="system.drawing.imaging" %>
    <%@ import namespace="system.drawing.drawing2d" %>
    <%

    ' initialise objects
    dim strFilename as string
    dim b as New System.Drawing.Bitmap(server.mappath("test.jpg"))
    dim g as graphics = graphics.fromimage(b)
    g.smoothingMode = smoothingMode.antiAlias
    dim stringFormat As New StringFormat()
    g.drawString("The Magic Word", New font("arial",3),systembrushes.windowtext,
    New pointF(2,2), stringFormat)

    ' Set the content type
    response.contenttype="image/jpeg"

    ' send the image to the viewer
    b.save(response.outputstream, b.rawformat)

    ' tidy up
    b.dispose()

    %>
    Richard, Jun 15, 2004
    #1
    1. Advertising

  2. Hi Richard,

    As for the "A Graphics object cannot be created from an image that has an
    indexed pixel format" error, it is caused by the Graphic object can't been
    created from some certain image format which contains indexed pixel. I've
    done some tests and one means is load the image and convert it into jpeg
    format first and then create Graphich object to manipulate it. For example,
    here is my test code:
    ==================================================
    Private Sub Output_Image(ByVal path As String)
    Try

    Dim img As Image = Image.FromFile(path)
    Dim stream As New System.IO.MemoryStream
    img.Save(stream, ImageFormat.Jpeg)
    Dim imgNew As Image = Image.FromStream(stream)

    Dim g As Graphics = Graphics.FromImage(imgNew)

    g.DrawString("MS Test", New Font("Verdana", 20,
    FontStyle.Bold), _
    New SolidBrush(Color.Beige), 0, 0)

    Response.Clear()
    Response.ContentType = "Image/jpeg"

    imgNew.Save(Response.OutputStream, ImageFormat.Jpeg)
    Response.End()


    img.Dispose()
    stream.Close()
    imgNew.Dispose()

    Catch ex As Exception
    Response.Write("<br>" + ex.Message)
    End Try

    End Sub
    =================================================

    Please have a try to see whether it works for you. Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    Steven Cheng[MSFT], Jun 16, 2004
    #2
    1. Advertising

  3. Richard

    Richard Guest

    thx a lot, this really helped me out.

    I got no errors and with some thinking I made sure ppl can't link to my
    files. (put a http://www. infront of url to view)

    digifan.nl/ShowGalleryImage.aspx?img=Jlz1wB3JMO636rzX3rGMrpOK7mbHBTzFQQ1OQ%2
    fJ5V3Y%3d

    and looking at the properties of the image it shows:

    digifan.nl/ShowFullImage.aspx?img=Jlz1wB3JMO636rzX3rGMrpOK7mbHBTzFQQ1OQ%2fJ5
    V3Y%3d

    which is only the image.

    Again thx, i am going to read a bit some more about the system.drawing and
    what it al can do but now i have an understanding of
    the general thing.

    Richard

    "Steven Cheng[MSFT]" <> schreef in bericht
    news:...
    > Hi Richard,
    >
    > As for the "A Graphics object cannot be created from an image that has an
    > indexed pixel format" error, it is caused by the Graphic object can't been
    > created from some certain image format which contains indexed pixel. I've
    > done some tests and one means is load the image and convert it into jpeg
    > format first and then create Graphich object to manipulate it. For

    example,
    > here is my test code:
    > ==================================================
    > Private Sub Output_Image(ByVal path As String)
    > Try
    >
    > Dim img As Image = Image.FromFile(path)
    > Dim stream As New System.IO.MemoryStream
    > img.Save(stream, ImageFormat.Jpeg)
    > Dim imgNew As Image = Image.FromStream(stream)
    >
    > Dim g As Graphics = Graphics.FromImage(imgNew)
    >
    > g.DrawString("MS Test", New Font("Verdana", 20,
    > FontStyle.Bold), _
    > New SolidBrush(Color.Beige), 0, 0)
    >
    > Response.Clear()
    > Response.ContentType = "Image/jpeg"
    >
    > imgNew.Save(Response.OutputStream, ImageFormat.Jpeg)
    > Response.End()
    >
    >
    > img.Dispose()
    > stream.Close()
    > imgNew.Dispose()
    >
    > Catch ex As Exception
    > Response.Write("<br>" + ex.Message)
    > End Try
    >
    > End Sub
    > =================================================
    >
    > Please have a try to see whether it works for you. Thanks.
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    > Get Preview at ASP.NET whidbey
    > http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    >
    Richard, Jun 16, 2004
    #3
  4. Richard

    velocity4anil

    Joined:
    Sep 19, 2007
    Messages:
    1
    Thanks Buddy...

    Thanks buddy.. been really helpful... :beerglas:
    velocity4anil, Sep 19, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Curt_C [MVP]
    Replies:
    3
    Views:
    14,276
    jharr100
    Aug 14, 2009
  2. Tampa .NET Koder
    Replies:
    0
    Views:
    504
    Tampa .NET Koder
    Jul 27, 2004
  3. b83503104
    Replies:
    2
    Views:
    313
    Rolf Magnus
    Nov 28, 2003
  4. BM

    JPG, PNG, GIF -> BMP

    BM, Sep 24, 2003, in forum: C Programming
    Replies:
    2
    Views:
    373
    Paul D. Boyle
    Sep 24, 2003
  5. b83503104
    Replies:
    2
    Views:
    514
    Rolf Magnus
    Nov 28, 2003
Loading...

Share This Page