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. General Programming
  3. C#
  4. very fast image conversion from jpg to bmp in c#

very fast image conversion from jpg to bmp in c#

Scheduled Pinned Locked Moved C#
csharpgraphicssysadminquestioncareer
14 Posts 6 Posters 2 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 Mahdipour
    wrote on last edited by
    #1

    Hi all developers, I have spent a lot of time to find a fast way for converting a stream of jpg images to bitmap format in c# (to use in a n image processing unit in my application). The conversion in my code is done as follows: using (MemoryStream ms = new MemoryStream(imageBytes)) { this.bqi.Bitmap = new Bitmap(ms); } in these code lines, imageBytes contains jpg image data ready to convert to bitmap format. These images are coming in from an IP camera as a web server. The frame rate is 15 frames per second, and converting 15 frames per second to Bitmap in this way takes too much time of CPU and the application is almost unable to do any other job when preview is on. Is there any workaround for this conversion without need to much cpu usage? Is there any solution on using directshow for converting jpg to bmp on gpu? any suggestions appreciated. thanks in advance

    ---------- Eric(M.M)

    N L T M 4 Replies Last reply
    0
    • M Mohammad Mahdipour

      Hi all developers, I have spent a lot of time to find a fast way for converting a stream of jpg images to bitmap format in c# (to use in a n image processing unit in my application). The conversion in my code is done as follows: using (MemoryStream ms = new MemoryStream(imageBytes)) { this.bqi.Bitmap = new Bitmap(ms); } in these code lines, imageBytes contains jpg image data ready to convert to bitmap format. These images are coming in from an IP camera as a web server. The frame rate is 15 frames per second, and converting 15 frames per second to Bitmap in this way takes too much time of CPU and the application is almost unable to do any other job when preview is on. Is there any workaround for this conversion without need to much cpu usage? Is there any solution on using directshow for converting jpg to bmp on gpu? any suggestions appreciated. thanks in advance

      ---------- Eric(M.M)

      N Offline
      N Offline
      Natza Mitzi
      wrote on last edited by
      #2

      Hi, Try to use the same bitmap object instead of creating a new one (I am assuming the images are the same size (resolution wise). Copy the jpeg data to the bitmap but do not use Get/Set pixel methods since they are slow. Instead, pin the image to memory and do the copy there. If that is not fast enough, do it in unmanaged code using C++, it will be much faster.

      Natza Mitzi Analysis Studio Statistical Analysis Software

      L 1 Reply Last reply
      0
      • N Natza Mitzi

        Hi, Try to use the same bitmap object instead of creating a new one (I am assuming the images are the same size (resolution wise). Copy the jpeg data to the bitmap but do not use Get/Set pixel methods since they are slow. Instead, pin the image to memory and do the copy there. If that is not fast enough, do it in unmanaged code using C++, it will be much faster.

        Natza Mitzi Analysis Studio Statistical Analysis Software

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        But what is that going to do? You still have to decode it..

        N 1 Reply Last reply
        0
        • M Mohammad Mahdipour

          Hi all developers, I have spent a lot of time to find a fast way for converting a stream of jpg images to bitmap format in c# (to use in a n image processing unit in my application). The conversion in my code is done as follows: using (MemoryStream ms = new MemoryStream(imageBytes)) { this.bqi.Bitmap = new Bitmap(ms); } in these code lines, imageBytes contains jpg image data ready to convert to bitmap format. These images are coming in from an IP camera as a web server. The frame rate is 15 frames per second, and converting 15 frames per second to Bitmap in this way takes too much time of CPU and the application is almost unable to do any other job when preview is on. Is there any workaround for this conversion without need to much cpu usage? Is there any solution on using directshow for converting jpg to bmp on gpu? any suggestions appreciated. thanks in advance

          ---------- Eric(M.M)

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          please tell us more: - what is the declaration of imageBytes? - what code is filling it? - what is the typical size (in pixels) of your images? - what is the typical size (in bytes) of your JPEG files? - how many images do you want to hold (ballpark figure)? :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
          [The QA section does it automatically now, I hope we soon get it on regular forums as well]


          M 1 Reply Last reply
          0
          • L Lost User

            But what is that going to do? You still have to decode it..

            N Offline
            N Offline
            Natza Mitzi
            wrote on last edited by
            #5

            I tested it and I do not think it is the encoding. Try to do the same with pre loaded bmp files in memory (eliminate the encoding) Here is the sample project I used: http://www.natzamitzi.com/imagetest.zip[^]

            Natza Mitzi Analysis Studio Statistical Analysis Software

            L 1 Reply Last reply
            0
            • N Natza Mitzi

              I tested it and I do not think it is the encoding. Try to do the same with pre loaded bmp files in memory (eliminate the encoding) Here is the sample project I used: http://www.natzamitzi.com/imagetest.zip[^]

              Natza Mitzi Analysis Studio Statistical Analysis Software

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              What? No I mean, if you just copy the JPG data into a bitmap, it is not decoded and therefore useless

              N 1 Reply Last reply
              0
              • M Mohammad Mahdipour

                Hi all developers, I have spent a lot of time to find a fast way for converting a stream of jpg images to bitmap format in c# (to use in a n image processing unit in my application). The conversion in my code is done as follows: using (MemoryStream ms = new MemoryStream(imageBytes)) { this.bqi.Bitmap = new Bitmap(ms); } in these code lines, imageBytes contains jpg image data ready to convert to bitmap format. These images are coming in from an IP camera as a web server. The frame rate is 15 frames per second, and converting 15 frames per second to Bitmap in this way takes too much time of CPU and the application is almost unable to do any other job when preview is on. Is there any workaround for this conversion without need to much cpu usage? Is there any solution on using directshow for converting jpg to bmp on gpu? any suggestions appreciated. thanks in advance

                ---------- Eric(M.M)

                T Offline
                T Offline
                The Zetta
                wrote on last edited by
                #7

                Whats wrong with save method of an Image instance!? I think its fast enough! just convert the stream into an Image and then : Image.Save("filename.bmp", System.Drawing.Imaging.ImageFormat.Bmp); P.S. : Code is corrected on January 27. thanks to Ennis Ray Lynch, Jr. ------------- Regards H.Maadani

                modified on Wednesday, January 27, 2010 11:30 AM

                E 1 Reply Last reply
                0
                • L Lost User

                  What? No I mean, if you just copy the JPG data into a bitmap, it is not decoded and therefore useless

                  N Offline
                  N Offline
                  Natza Mitzi
                  wrote on last edited by
                  #8

                  Look at the previous message with the sample code. I conducted a test with jpeg images and it seems like the decoding does not take that much CPU. I suggested that you try to isolate the problem by using a set of predefined images (eliminate the decoding) and see whether the CPU usage continues.

                  Natza Mitzi Analysis Studio Statistical Analysis Software

                  L 1 Reply Last reply
                  0
                  • N Natza Mitzi

                    Look at the previous message with the sample code. I conducted a test with jpeg images and it seems like the decoding does not take that much CPU. I suggested that you try to isolate the problem by using a set of predefined images (eliminate the decoding) and see whether the CPU usage continues.

                    Natza Mitzi Analysis Studio Statistical Analysis Software

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Ok it makes more sense now, I hope the OP is reading this as well

                    1 Reply Last reply
                    0
                    • T The Zetta

                      Whats wrong with save method of an Image instance!? I think its fast enough! just convert the stream into an Image and then : Image.Save("filename.bmp", System.Drawing.Imaging.ImageFormat.Bmp); P.S. : Code is corrected on January 27. thanks to Ennis Ray Lynch, Jr. ------------- Regards H.Maadani

                      modified on Wednesday, January 27, 2010 11:30 AM

                      E Offline
                      E Offline
                      Ennis Ray Lynch Jr
                      wrote on last edited by
                      #10

                      Your code would save a jpeg with a file extension of bmp and not save the file as a bitmap.

                      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

                      T 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        please tell us more: - what is the declaration of imageBytes? - what code is filling it? - what is the typical size (in pixels) of your images? - what is the typical size (in bytes) of your JPEG files? - how many images do you want to hold (ballpark figure)? :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                        [The QA section does it automatically now, I hope we soon get it on regular forums as well]


                        M Offline
                        M Offline
                        Mohammad Mahdipour
                        wrote on last edited by
                        #11

                        thanks for all suggestions and solutions, - imageBytes is exactly like a jpg file that is loaded to the memory (for example using File.ReadAllBytes(), but here the images are coming in from a stream not from files) - The images are 1600x1200 (it seems a little large!) - The images are about 157000 bytes - I have an AMD Athlon 64 X2 Dual Core Processor 6000+ 3.01 GHz 2 GB RAM, converting these jpg frames to bitmap takes approzmately 40% of my dual core cpu (each frame takes about 60ms to be converted to a Bitmap object in bmp format), the image processing algorithms that I apply on the images take too much cpu, but if converting to Bitmap take 20% of cpu, it is ideal for me. - I need at least 10 to 15 frames per second to be converted to Bitmap. BTW, it was my first post in the code project with code, so I forgot to format code lines!

                        ---------- Eric(M.M)

                        L 1 Reply Last reply
                        0
                        • M Mohammad Mahdipour

                          Hi all developers, I have spent a lot of time to find a fast way for converting a stream of jpg images to bitmap format in c# (to use in a n image processing unit in my application). The conversion in my code is done as follows: using (MemoryStream ms = new MemoryStream(imageBytes)) { this.bqi.Bitmap = new Bitmap(ms); } in these code lines, imageBytes contains jpg image data ready to convert to bitmap format. These images are coming in from an IP camera as a web server. The frame rate is 15 frames per second, and converting 15 frames per second to Bitmap in this way takes too much time of CPU and the application is almost unable to do any other job when preview is on. Is there any workaround for this conversion without need to much cpu usage? Is there any solution on using directshow for converting jpg to bmp on gpu? any suggestions appreciated. thanks in advance

                          ---------- Eric(M.M)

                          M Offline
                          M Offline
                          Mohammad Mahdipour
                          wrote on last edited by
                          #12

                          Any idea on using DirectShow to convert images? because as you know, when you watch a video on your computer, it does not take too cpu time because decoding movie frames is one in gpu, not on cpu. Is there any way on using directshow to convert single frame images? I have researched alot on this idea, but all the things I've found is converting movie from one format to another using directshow. Any suggestions appreciated.

                          ---------- Eric(M.M)

                          1 Reply Last reply
                          0
                          • E Ennis Ray Lynch Jr

                            Your code would save a jpeg with a file extension of bmp and not save the file as a bitmap.

                            Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

                            T Offline
                            T Offline
                            The Zetta
                            wrote on last edited by
                            #13

                            You're right! my bad. it must be : System.Drawing.Imaging.ImageFormat.Bmp I'll correct it now.

                            1 Reply Last reply
                            0
                            • M Mohammad Mahdipour

                              thanks for all suggestions and solutions, - imageBytes is exactly like a jpg file that is loaded to the memory (for example using File.ReadAllBytes(), but here the images are coming in from a stream not from files) - The images are 1600x1200 (it seems a little large!) - The images are about 157000 bytes - I have an AMD Athlon 64 X2 Dual Core Processor 6000+ 3.01 GHz 2 GB RAM, converting these jpg frames to bitmap takes approzmately 40% of my dual core cpu (each frame takes about 60ms to be converted to a Bitmap object in bmp format), the image processing algorithms that I apply on the images take too much cpu, but if converting to Bitmap take 20% of cpu, it is ideal for me. - I need at least 10 to 15 frames per second to be converted to Bitmap. BTW, it was my first post in the code project with code, so I forgot to format code lines!

                              ---------- Eric(M.M)

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #14

                              Hi, I ran a couple of experiments, with a set of 100 JPEG images, each larger than 2200*1800 ("original"). I also converted them to a set of 1600*1200 ("medium") and another set of 800*600 ("small"). The test consisted of animating those images to a PictureBox as fast as possible, basically with this code:

                              foreach (string s in list) {
                              	Image oldImage=pb.Image;
                              	if (oldImage!=null) oldImage.Dispose();
                              	using (FileStream stream=File.Open(s, FileMode.Open)) {
                              		Bitmap bm=new Bitmap(stream);
                              		pb.Image=bm;
                              		Application.DoEvents(); // update the PictureBox
                              	}
                              }
                              

                              And the time required on a laptop (with an Intel 2.4GHz Core2 Duo) was 18/8/2 seconds for original/medium/small images, telling me the image size (in pixels) is of utmost importance. So, if at all possible, I recommend you tell your camera or webcam to output lower-resolution images. BTW: the same experiment with bitmap images (*.BMP) is terribly slow, due to the size of the files involved. :)

                              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                              [The QA section does it automatically now, I hope we soon get it on regular forums as well]


                              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