Problem with indexed pixel format/

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jty202

    Problem with indexed pixel format/

    I know alot of people have the problem with indexed pixel format. I hope
    someone can show me the solution to this.I am have problem with graphics
    with the following code giving the error:

    "A Graphics object cannot be created from an image that has an indexed pixel
    format."

    'My code


    <%@ Page Language="vb" %>
    <%@ import namespace="syst em.drawing" %>
    <%@ import namespace="syst em.drawing.imag ing" %>
    <%@ import namespace="syst em.drawing.draw ing2d" %>


    <%

    ' initialise the web object
    Dim webC As New System.Net.WebC lient()

    ' set the URL
    Dim strFilename as string


    strFilename="ht tp://www.domain.com/graph.gif"

    ' create a bitmap based on the image from the URL
    Dim g = New System.Drawing. Bitmap(webC.Ope nRead(strFilena me))


    response.conten ttype="image/gif"

    dim gr as graphics = graphics.fromim age(g) ' create a New graphic object
    from the above bmp

    gr.smoothingMod e = smoothingMode.a ntiAlias ' antialias objects

    '' draw the number on the image canvas in verdana 10pt font bold
    gr.drawString(" ABC", New
    font("verdana", 14,fontstyle.bo ld),systembrush es.windowtext, New pointF(2,2))


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

    ' tidy up
    g.dispose()
    gr.dispose()
    webC = Nothing

    %>



  • Bob Powell [MVP]

    #2
    Re: Problem with indexed pixel format/

    This is intended behaviour. The error "A Graphics object cannot be created
    from an image that has an indexed pixel format." says it all.

    You need to create an image the same size as the original, get the Graphics
    object for it, draw the original onto the new Graphics object, draw your
    number and then save the newly created bitmap to the response stream.

    --
    Bob Powell [MVP]
    Visual C#, System.Drawing

    Find great Windows Forms articles in Windows Forms Tips and Tricks


    Answer those GDI+ questions with the GDI+ FAQ


    All new articles provide code in C# and VB.NET.
    Subscribe to the RSS feeds provided and never miss a new article.





    "jty202" <jty202@gmail.c om> wrote in message
    news:e13tjuZAFH A.3700@tk2msftn gp13.phx.gbl...[color=blue]
    >I know alot of people have the problem with indexed pixel format. I hope
    > someone can show me the solution to this.I am have problem with graphics
    > with the following code giving the error:
    >
    > "A Graphics object cannot be created from an image that has an indexed
    > pixel
    > format."
    >
    > 'My code
    >
    >
    > <%@ Page Language="vb" %>
    > <%@ import namespace="syst em.drawing" %>
    > <%@ import namespace="syst em.drawing.imag ing" %>
    > <%@ import namespace="syst em.drawing.draw ing2d" %>
    >
    >
    > <%
    >
    > ' initialise the web object
    > Dim webC As New System.Net.WebC lient()
    >
    > ' set the URL
    > Dim strFilename as string
    >
    >
    > strFilename="ht tp://www.domain.com/graph.gif"
    >
    > ' create a bitmap based on the image from the URL
    > Dim g = New System.Drawing. Bitmap(webC.Ope nRead(strFilena me))
    >
    >
    > response.conten ttype="image/gif"
    >
    > dim gr as graphics = graphics.fromim age(g) ' create a New graphic object
    > from the above bmp
    >
    > gr.smoothingMod e = smoothingMode.a ntiAlias ' antialias objects
    >
    > '' draw the number on the image canvas in verdana 10pt font bold
    > gr.drawString(" ABC", New
    > font("verdana", 14,fontstyle.bo ld),systembrush es.windowtext, New
    > pointF(2,2))
    >
    >
    > ' send the image to the viewer
    > g.save(response .outputstream, g.rawformat)
    >
    > ' tidy up
    > g.dispose()
    > gr.dispose()
    > webC = Nothing
    >
    > %>
    >
    >
    >[/color]


    Comment

    Working...