Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Generating JPEG images on-the-fly

Generating JPEG images on-the-fly

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdatabasecomsysadmin
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mohammad A Gdeisat
    wrote on last edited by
    #1

    Hi all, I have a database that conatins a list of quotations like this: ID Quote 1 Hi, how are you? 2 Who is this? ... ... Let's assume that my site is www.myquotes.com, the site doesn't have any pictures stored on it, only the database and the site code. I want to create a website that works as follows: When a user requests the address "www.myquotes.com/1.jpg" (1 is the ID of the Quote). I want to my ASP .NET application to do the following sequence of events: 1. Extract Quote #1 from the database. 2. Generate a JPEG image that has the writing "Hi, how are you?" written on it 3. Return this image back to the client, so the client will receive a picture that has a writing on it. so from the client's point of view, he will just request a JPEG image and receive it. My Questions: 1. Can this be done in ASP.NET (I suppose yes). 2. Are there any .NET compatible tools that can help in dynamically generating JPEG images that contain text on them. 3. Is the process of JPEG image generation time-consuming for the web-server? In other words, if I start to get hundreds of image requests per second will this bring my web application down? 4. Any other tips for doing this? Regards, Mohammad

    And ever has it been that love knows not its own depth until the hour of separation

    P 1 Reply Last reply
    0
    • M Mohammad A Gdeisat

      Hi all, I have a database that conatins a list of quotations like this: ID Quote 1 Hi, how are you? 2 Who is this? ... ... Let's assume that my site is www.myquotes.com, the site doesn't have any pictures stored on it, only the database and the site code. I want to create a website that works as follows: When a user requests the address "www.myquotes.com/1.jpg" (1 is the ID of the Quote). I want to my ASP .NET application to do the following sequence of events: 1. Extract Quote #1 from the database. 2. Generate a JPEG image that has the writing "Hi, how are you?" written on it 3. Return this image back to the client, so the client will receive a picture that has a writing on it. so from the client's point of view, he will just request a JPEG image and receive it. My Questions: 1. Can this be done in ASP.NET (I suppose yes). 2. Are there any .NET compatible tools that can help in dynamically generating JPEG images that contain text on them. 3. Is the process of JPEG image generation time-consuming for the web-server? In other words, if I start to get hundreds of image requests per second will this bring my web application down? 4. Any other tips for doing this? Regards, Mohammad

      And ever has it been that love knows not its own depth until the hour of separation

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      Mohammad A Gdeisat wrote:

      1. Can this be done in ASP.NET (I suppose yes).

      Correct

      Mohammad A Gdeisat wrote:

      2. Are there any .NET compatible tools that can help in dynamically generating JPEG images that contain text on them.

      The .NET BCL exposes functionality that allows images to be dynamically generated. Create an instance of a Bitmap[^] class, use the Graphics.DrawString[^] method to draw the text onto the image. Call Bitmap.Save and save the bitmap data to a MemoryStream. Finally, and write the contents of the MemoryStream out to the browser by calling HttpResponse.BinaryWrite.

      Mohammad A Gdeisat wrote:

      3. Is the process of JPEG image generation time-consuming for the web-server? In other words, if I start to get hundreds of image requests per second will this bring my web application down?

      I'm not an expert on this, but I would have thought that generating images is somewhat performance-intensive. You should run some tests to investigate this.

      Mohammad A Gdeisat wrote:

      4. Any other tips for doing this?

      Nothing else I can think of.

      Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

      M S 2 Replies Last reply
      0
      • P pmarfleet

        Mohammad A Gdeisat wrote:

        1. Can this be done in ASP.NET (I suppose yes).

        Correct

        Mohammad A Gdeisat wrote:

        2. Are there any .NET compatible tools that can help in dynamically generating JPEG images that contain text on them.

        The .NET BCL exposes functionality that allows images to be dynamically generated. Create an instance of a Bitmap[^] class, use the Graphics.DrawString[^] method to draw the text onto the image. Call Bitmap.Save and save the bitmap data to a MemoryStream. Finally, and write the contents of the MemoryStream out to the browser by calling HttpResponse.BinaryWrite.

        Mohammad A Gdeisat wrote:

        3. Is the process of JPEG image generation time-consuming for the web-server? In other words, if I start to get hundreds of image requests per second will this bring my web application down?

        I'm not an expert on this, but I would have thought that generating images is somewhat performance-intensive. You should run some tests to investigate this.

        Mohammad A Gdeisat wrote:

        4. Any other tips for doing this?

        Nothing else I can think of.

        Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

        M Offline
        M Offline
        Mohammad A Gdeisat
        wrote on last edited by
        #3

        Thanks mate for the reply, just another related question: When a client requests the web address "www.myquotes.com/1.jpg" can ASP .NET handle the request? I mean that IIS might not handle the HTTP request because the extension of the file is JPG not ASPX. Is there a way to make my web application handle HTTP requests for JPG file types? btw, I will run my application on public servers so I might not be able to set IIS configuration. Thanks Mohammad

        And ever has it been that love knows not its own depth until the hour of separation

        N 1 Reply Last reply
        0
        • M Mohammad A Gdeisat

          Thanks mate for the reply, just another related question: When a client requests the web address "www.myquotes.com/1.jpg" can ASP .NET handle the request? I mean that IIS might not handle the HTTP request because the extension of the file is JPG not ASPX. Is there a way to make my web application handle HTTP requests for JPG file types? btw, I will run my application on public servers so I might not be able to set IIS configuration. Thanks Mohammad

          And ever has it been that love knows not its own depth until the hour of separation

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          Mohammad A Gdeisat wrote:

          When a client requests the web address "www.myquotes.com/1.jpg" can ASP .NET handle the request? I mean that IIS might not handle the HTTP request because the extension of the file is JPG not ASPX.

          If JPG extension is mapped to aspnet_isapi.dll in the server(IIS), it can handle that. BTW, why do you want to handle that ? I guess you need to write the string even though they are accessing it directly ? Correct ? For doing that, you can map JPG extension to HTTPForbiddenHandler. So when user requests the files directly, it will show "this type of pages are not served".

          Mohammad A Gdeisat wrote:

          I will run my application on public servers so I might not be able to set IIS configuration.

          AFAIK, server providers will do this for you if you place a request. I have done the same with my provider.

          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions

          1 Reply Last reply
          0
          • P pmarfleet

            Mohammad A Gdeisat wrote:

            1. Can this be done in ASP.NET (I suppose yes).

            Correct

            Mohammad A Gdeisat wrote:

            2. Are there any .NET compatible tools that can help in dynamically generating JPEG images that contain text on them.

            The .NET BCL exposes functionality that allows images to be dynamically generated. Create an instance of a Bitmap[^] class, use the Graphics.DrawString[^] method to draw the text onto the image. Call Bitmap.Save and save the bitmap data to a MemoryStream. Finally, and write the contents of the MemoryStream out to the browser by calling HttpResponse.BinaryWrite.

            Mohammad A Gdeisat wrote:

            3. Is the process of JPEG image generation time-consuming for the web-server? In other words, if I start to get hundreds of image requests per second will this bring my web application down?

            I'm not an expert on this, but I would have thought that generating images is somewhat performance-intensive. You should run some tests to investigate this.

            Mohammad A Gdeisat wrote:

            4. Any other tips for doing this?

            Nothing else I can think of.

            Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

            S Offline
            S Offline
            shipstech
            wrote on last edited by
            #5

            Sir, Thank you for your answer. If I may, a follow on question. I have vb.net console application that draws with GDI+ and saves it to a .jpg every second. My .aspx web page auto refreshes every second with the newly saved .jpg included. I would like to do this without saving to hard disk. I would like the console application to simply create a memory stream as you have suggested but only see option for a MemoryBMP. Can a .aspx page use a bitmap from memory of another process? Any suggestions? Thank you.

            P 1 Reply Last reply
            0
            • S shipstech

              Sir, Thank you for your answer. If I may, a follow on question. I have vb.net console application that draws with GDI+ and saves it to a .jpg every second. My .aspx web page auto refreshes every second with the newly saved .jpg included. I would like to do this without saving to hard disk. I would like the console application to simply create a memory stream as you have suggested but only see option for a MemoryBMP. Can a .aspx page use a bitmap from memory of another process? Any suggestions? Thank you.

              P Offline
              P Offline
              pmarfleet
              wrote on last edited by
              #6

              shipstech wrote:

              Can a .aspx page use a bitmap from memory of another process?

              I don't think so. It sounds like your current solution is overcomplicated though. Is it necessary for your console app to create the files? Your ASP.NET web application could dynamically create the images in memory and stream them back to the client. Search Google[^] and you will find plenty of examples of how to do what I suggested.

              Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

              S 1 Reply Last reply
              0
              • P pmarfleet

                shipstech wrote:

                Can a .aspx page use a bitmap from memory of another process?

                I don't think so. It sounds like your current solution is overcomplicated though. Is it necessary for your console app to create the files? Your ASP.NET web application could dynamically create the images in memory and stream them back to the client. Search Google[^] and you will find plenty of examples of how to do what I suggested.

                Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

                S Offline
                S Offline
                shipstech
                wrote on last edited by
                #7

                Thank you for your quick reply. The console app is a stripped down version of a windows forms app that has a nice interface. I was looking for a quick way to provide the same info across the LAN on browsers and thought I'd try my first asp.net project. Saving the JPG and serving it up on a web page was a quick and dirty way for proof of concept. I am now exploring adding an HTTPlistener to the console app, making it a page server so no passing of image required. I'll see how it goes and may buckle down and port the GDI+ code to asp.net as you suggest. Thanks again.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups