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. how to speed up GDI+ without using pointer

how to speed up GDI+ without using pointer

Scheduled Pinned Locked Moved C#
graphicsperformancecsharpwinformscom
11 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.
  • Z Offline
    Z Offline
    zecodela
    wrote on last edited by
    #1

    hi, i read an article by Christian Graus http://www.codeproject.com/cs/media/displacementfilters.asp in which, he showed how to do filter with C#. in my case, i need to some analysis on image in order to do some segmentation, binarization stuffs. i used Bitmap.GetPixel() to access the bitmap. however, it is extremely slow comparing Christian method using Bitmap.Scan0() to get the pointer of bitmap and go thro' the structure in system memory. i want to know any method prevent using pointer in order to access bitmap in high speed? i want to know any other method to speed up the process. and, i don't want to handle the structure of each different image format after employed pointer access. thanks, jim

    A F 2 Replies Last reply
    0
    • Z zecodela

      hi, i read an article by Christian Graus http://www.codeproject.com/cs/media/displacementfilters.asp in which, he showed how to do filter with C#. in my case, i need to some analysis on image in order to do some segmentation, binarization stuffs. i used Bitmap.GetPixel() to access the bitmap. however, it is extremely slow comparing Christian method using Bitmap.Scan0() to get the pointer of bitmap and go thro' the structure in system memory. i want to know any method prevent using pointer in order to access bitmap in high speed? i want to know any other method to speed up the process. and, i don't want to handle the structure of each different image format after employed pointer access. thanks, jim

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      Can't you build a wrapper arround the unsafe code? I mean create a class which does that. I have done it, and if you say, I can paste the whole code here too... its MUCH faster then the Bitmap provided by .NET. Actually in .NET they have written a thin wrapper arround the old GDI so thats why it is too slow. Anyway, let me know if you want to get password. mE

      F 1 Reply Last reply
      0
      • A Anonymous

        Can't you build a wrapper arround the unsafe code? I mean create a class which does that. I have done it, and if you say, I can paste the whole code here too... its MUCH faster then the Bitmap provided by .NET. Actually in .NET they have written a thin wrapper arround the old GDI so thats why it is too slow. Anyway, let me know if you want to get password. mE

        F Offline
        F Offline
        fadee
        wrote on last edited by
        #3

        Sorry - that was me - but not signed in. Now I am :) --------------------- A gasp of breath, A sudden death: The tale begun. A rustled page Passes an age: The tale is done.

        1 Reply Last reply
        0
        • Z zecodela

          hi, i read an article by Christian Graus http://www.codeproject.com/cs/media/displacementfilters.asp in which, he showed how to do filter with C#. in my case, i need to some analysis on image in order to do some segmentation, binarization stuffs. i used Bitmap.GetPixel() to access the bitmap. however, it is extremely slow comparing Christian method using Bitmap.Scan0() to get the pointer of bitmap and go thro' the structure in system memory. i want to know any method prevent using pointer in order to access bitmap in high speed? i want to know any other method to speed up the process. and, i don't want to handle the structure of each different image format after employed pointer access. thanks, jim

          F Offline
          F Offline
          fadee
          wrote on last edited by
          #4

          Here is the code... Instead of making Bitmap object, make object of this class, constructors are more then enough :) I claim that it VERY FAST as compared to GDI of .NET... the operation done by GDI in 9 seconds will be done in 3 seconds.. with same ease! Don foget to compile with /unsafe switch... A structure me using... using System; namespace ImageProcessor.Imaging { /// /// This structure contains structure information to be used in Facer.Imaging.BitmapImage.FaceRImage Class. /// unsafe public struct PixelData { public byte Red; public byte Green; public byte Blue; } } THE CLASS using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; namespace ImageProcessor.Imaging { /// /// Summary description for BitmapImage. /// public class Image { private String File; public String FileName { get { return File; } } private int H; private int W; public int Height { get { return H; } } public int Width { get { return W; } } private Bitmap img; public Bitmap BitmapImage { get { return img; } } private BitmapData Data; unsafe private Byte* PBase = null; private int ByteWidth = 0; Rectangle Bounds; private Image() { } unsafe public Image(String Path) { File = Path; img = new Bitmap(File); if(img.PixelFormat != PixelFormat.Format24bppRgb) throw new ImageProcessor.Exceptions.Imaging.InvalidImageFormatException("Invalid Image Format. Expecting: Format24bppRgb Provided: " + img.PixelFormat.ToString()); Data = null; H = this.img.Height; W = this.img.Width; GraphicsUnit Unit = GraphicsUnit.Pixel; RectangleF BoundsF = img.GetBounds(ref Unit); Bounds = new Rectangle((int) BoundsF.X,(int) BoundsF.Y,(int) BoundsF.Width,(int) BoundsF.Height); ByteWidth = (int) BoundsF.Width * sizeof(PixelData); if (ByteWidth % 4 != 0) ByteWidth = 4 * (ByteWidth / 4 + 1); } unsafe public Image(int w,int h) { File = this.GetHashCode().ToString() + ".bmp"; img = new Bitmap(w,h,PixelFormat.Format24bppRgb); Data = null; H = this.img.Height; W = this.img.Width; GraphicsUnit Unit = GraphicsUnit.Pixel; RectangleF BoundsF = img.GetBounds(ref Unit); Bounds = new Rectangle((int) BoundsF.X,(int) BoundsF.Y,(int) BoundsF

          Z 1 Reply Last reply
          0
          • F fadee

            Here is the code... Instead of making Bitmap object, make object of this class, constructors are more then enough :) I claim that it VERY FAST as compared to GDI of .NET... the operation done by GDI in 9 seconds will be done in 3 seconds.. with same ease! Don foget to compile with /unsafe switch... A structure me using... using System; namespace ImageProcessor.Imaging { /// /// This structure contains structure information to be used in Facer.Imaging.BitmapImage.FaceRImage Class. /// unsafe public struct PixelData { public byte Red; public byte Green; public byte Blue; } } THE CLASS using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; namespace ImageProcessor.Imaging { /// /// Summary description for BitmapImage. /// public class Image { private String File; public String FileName { get { return File; } } private int H; private int W; public int Height { get { return H; } } public int Width { get { return W; } } private Bitmap img; public Bitmap BitmapImage { get { return img; } } private BitmapData Data; unsafe private Byte* PBase = null; private int ByteWidth = 0; Rectangle Bounds; private Image() { } unsafe public Image(String Path) { File = Path; img = new Bitmap(File); if(img.PixelFormat != PixelFormat.Format24bppRgb) throw new ImageProcessor.Exceptions.Imaging.InvalidImageFormatException("Invalid Image Format. Expecting: Format24bppRgb Provided: " + img.PixelFormat.ToString()); Data = null; H = this.img.Height; W = this.img.Width; GraphicsUnit Unit = GraphicsUnit.Pixel; RectangleF BoundsF = img.GetBounds(ref Unit); Bounds = new Rectangle((int) BoundsF.X,(int) BoundsF.Y,(int) BoundsF.Width,(int) BoundsF.Height); ByteWidth = (int) BoundsF.Width * sizeof(PixelData); if (ByteWidth % 4 != 0) ByteWidth = 4 * (ByteWidth / 4 + 1); } unsafe public Image(int w,int h) { File = this.GetHashCode().ToString() + ".bmp"; img = new Bitmap(w,h,PixelFormat.Format24bppRgb); Data = null; H = this.img.Height; W = this.img.Width; GraphicsUnit Unit = GraphicsUnit.Pixel; RectangleF BoundsF = img.GetBounds(ref Unit); Bounds = new Rectangle((int) BoundsF.X,(int) BoundsF.Y,(int) BoundsF

            Z Offline
            Z Offline
            zecodela
            wrote on last edited by
            #5

            thank you very much! let me try it! big thanks!

            F 1 Reply Last reply
            0
            • Z zecodela

              thank you very much! let me try it! big thanks!

              F Offline
              F Offline
              fadee
              wrote on last edited by
              #6

              Also... please don look at the name space names... like Facer.Imaging.BitmapImage.FaceRImage.. okay? Another thing, the SetPixelAtChecked is slow as compared to SetPixelAt... coz in checked, the bounds are checked and is good for test-your-algo purpose. if you have any problem, just put it here I will answer it. Also... a request, do let me know whether it helped you or not :) mE --------------------- A gasp of breath, A sudden death: The tale begun. A rustled page Passes an age: The tale is done.

              R Z 3 Replies Last reply
              0
              • F fadee

                Also... please don look at the name space names... like Facer.Imaging.BitmapImage.FaceRImage.. okay? Another thing, the SetPixelAtChecked is slow as compared to SetPixelAt... coz in checked, the bounds are checked and is good for test-your-algo purpose. if you have any problem, just put it here I will answer it. Also... a request, do let me know whether it helped you or not :) mE --------------------- A gasp of breath, A sudden death: The tale begun. A rustled page Passes an age: The tale is done.

                R Offline
                R Offline
                ralfoide
                wrote on last edited by
                #7

                Your class is very helpful, thank you! I was wondering myself how to do that in C# Best regards -- Ralf

                1 Reply Last reply
                0
                • F fadee

                  Also... please don look at the name space names... like Facer.Imaging.BitmapImage.FaceRImage.. okay? Another thing, the SetPixelAtChecked is slow as compared to SetPixelAt... coz in checked, the bounds are checked and is good for test-your-algo purpose. if you have any problem, just put it here I will answer it. Also... a request, do let me know whether it helped you or not :) mE --------------------- A gasp of breath, A sudden death: The tale begun. A rustled page Passes an age: The tale is done.

                  Z Offline
                  Z Offline
                  zecodela
                  wrote on last edited by
                  #8

                  hi, would u tell me how can i enable /unsafe in VS IDE? i don't know why i can't see Build Property Page that the help file tell me..! regards, jim

                  Z 1 Reply Last reply
                  0
                  • Z zecodela

                    hi, would u tell me how can i enable /unsafe in VS IDE? i don't know why i can't see Build Property Page that the help file tell me..! regards, jim

                    Z Offline
                    Z Offline
                    zecodela
                    wrote on last edited by
                    #9

                    oh! i found that page editing the unsafe options thanks!

                    1 Reply Last reply
                    0
                    • F fadee

                      Also... please don look at the name space names... like Facer.Imaging.BitmapImage.FaceRImage.. okay? Another thing, the SetPixelAtChecked is slow as compared to SetPixelAt... coz in checked, the bounds are checked and is good for test-your-algo purpose. if you have any problem, just put it here I will answer it. Also... a request, do let me know whether it helped you or not :) mE --------------------- A gasp of breath, A sudden death: The tale begun. A rustled page Passes an age: The tale is done.

                      Z Offline
                      Z Offline
                      zecodela
                      wrote on last edited by
                      #10

                      hi, your class is faster than framework's Bitmap class but i found that Christ's method still much much much faster... i did greyscale for 1600x1200 image with 20s. but, christian's application used less than 1s . just a little report. i will check the code to find the reason of this.

                      F 1 Reply Last reply
                      0
                      • Z zecodela

                        hi, your class is faster than framework's Bitmap class but i found that Christ's method still much much much faster... i did greyscale for 1600x1200 image with 20s. but, christian's application used less than 1s . just a little report. i will check the code to find the reason of this.

                        F Offline
                        F Offline
                        fadee
                        wrote on last edited by
                        #11

                        Yes, this is an old story... fight between ease of programmer and the performance. You get best possible speed when you write code in assembly. But you can't write big programs in it. Since machines are faster now, so we compromise performance on ease. Anyway, thank for telling me :) Good Day; mE --------------------- A gasp of breath, A sudden death: The tale begun. A rustled page Passes an age: The tale is done.

                        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