none
How to create subimage?

    Question

  • Hello,

    Let's say I have a jpj file. I want to create a subimage of that, not simply display it in a picturebox or draw it on a panel but have it as a set of subpixels in memory.

    E.g. a .jpg file has 1200 x 800 pixels. I want to create an Image img1 = new Bitmap ( 200, 100 ) where the location of this subimage is NOT necesserily at (0, 0) but at any given point in the .jpg rectangle.

    How can I do it?

    Thanks.


    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Monday, September 13, 2010 6:30 PM

Answers

All replies

  • Hello,

    In this thread I asked a question as to how to take a slice of an original bmp file and project it in a pictureBox. Now it works fine. There is a little problem, though. It looks as the picture-subset of the original is magnified. I would have hard time describing it but basicaly the size of the projection is larger in both dimensions than the original area with the same content by about a factor of two. I don't need it. I use the projection to verify the fact that the location and size of the subset are correct. Thus I need a lower quality picture for viewing. In shor tI need a sort of interim compression, perhaps rendering a lower quality of picture. How can I do it?

    This is my simple code for clarity.

    private void DrawSubset ( )
            {
                Sph sph = new Sph ( );
                this.image1 = sph.ConvertJpgToBitmap ( @"E:\...\P1000435.jpg" );                  
                if ( this.txtXcoord.Text != "" & this.txtYcoord.Text != ""  & this.txtWindowWidth.Text != "" & this.txtWindowHeight.Text != "" )
                {
                    int width = int.Parse ( this.txtWindowWidth.Text );
                    this.windowWidth = width;
                    int height = int.Parse ( this.txtWindowHeight.Text );
                    this.windowHeight = height;
                    this.pictureBox1.Image = new Bitmap ( width, height );
                    int xCoord = int.Parse ( this.txtXcoord.Text );
                    this.locationX = xCoord;
                    int yCoord = int.Parse ( this.txtYcoord.Text );
                    this.locationY = yCoord;
                    PointF location = new PointF ( xCoord, yCoord );
                    Graphics.FromImage ( this.pictureBox1.Image ).DrawImage ( image1, 0 , 0, new RectangleF ( location.X, location.Y, width, height ), GraphicsUnit.Pixel );
                }
            }                                               // DrawSubset

    location.X, location.Y and width and height determine the position of the rectangle on the original .jpg file.

    this.windowWidth and others are used to manipulate the position and the size of the window thru scroll bars.

    Thanks.


    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    • Merged by Figo Fei Wednesday, September 15, 2010 7:06 AM same question
    Friday, September 10, 2010 1:01 PM
  • Hi,

    I don't know, if I got you right here, but to use a low_quality_interim_image you could create a secont image which is smaller than the original and draw the entire original to it, and then - for display usage - draw the small second picture in desired size.

    Have a look at hte Graphics.DrawImage Method:

    http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage.aspx

    especially:

    http://msdn.microsoft.com/en-us/library/ktyfbs10.aspx

    btw:

    > Graphics.FromImage ( this.pictureBox1.Image ).DrawImage ( image1, 0 , 0, new RectangleF ( location.X, location.Y, width, > height ), GraphicsUnit.Pixel );

    I recommend to

    - instanciate the Graphics-Object

    - use it

    - and Dispose it

    Graphics gx = Graphics.FromImage([IMGVARIABLE]);
    gx.DrawImage([PARAMS]);
    gx.Dispose();
    gx = null;
    

    or use it in a "using"-block.

    regards,

      Thorsten

    Friday, September 10, 2010 5:29 PM
  • How did you set pictureBox1.SizeMode?
    Saturday, September 11, 2010 1:26 AM
  • You know that image has a standard method for creating thumbnails (which is just a word because you can set the shrinking size)

    http://msdn.microsoft.com/en-us/library/ets0sayh(v=VS.100).aspx


    Success
    Cor
    • Proposed as answer by Figo Fei Wednesday, September 15, 2010 7:12 AM
    • Marked as answer by Figo Fei Friday, September 17, 2010 7:52 AM
    Saturday, September 11, 2010 8:39 AM
  • How did you set pictureBox1.SizeMode?

    I did not. I used defaults. I am doing it on the cheep without serious investigation being too overwhelmed with some other stuff. It is a sort of a relaxation for me at this point but soon it will become a major project. Yes, I noticed that mode yesterday and decided to use it.
    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Saturday, September 11, 2010 2:39 PM
  • You know that image has a standard method for creating thumbnails (which is just a word because you can set the shrinking size)

    http://msdn.microsoft.com/en-us/library/ets0sayh(v=VS.100).aspx


    Success
    Cor


    That may be a very good idea. I will try it today. But I will have to create a separate jpg for the subset first, correct?

    Thanks.


    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Saturday, September 11, 2010 2:44 PM
  • I want to try this algorithm first:

            private void SaveJPGWithCompressionSetting ( Image image, string szFileName, long lCompression )
            {
                EncoderParameters eps = new EncoderParameters ( 1 );
                eps.Param[ 0 ] = new EncoderParameter ( System.Drawing.Imaging.Encoder.Quality, lCompression );
                ImageCodecInfo ici = GetEncoderInfo ( "image/jpeg" );
                image.Save ( szFileName, ici, eps );
            }                                               // SaveJPGWithCompressionSetting


    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Saturday, September 11, 2010 2:49 PM
  • SizeMode is not applicable to what I want.
    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Monday, September 13, 2010 2:14 PM
  • Hello,

    Let's say I have a jpj file. I want to create a subimage of that, not simply display it in a picturebox or draw it on a panel but have it as a set of subpixels in memory.

    E.g. a .jpg file has 1200 x 800 pixels. I want to create an Image img1 = new Bitmap ( 200, 100 ) where the location of this subimage is NOT necesserily at (0, 0) but at any given point in the .jpg rectangle.

    How can I do it?

    It seems I can create this empy bitmap and assign propertyItems but not pixels.

    Thanks.


    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Monday, September 13, 2010 6:32 PM
  • This process is called Image Cropping.

    You can use this function CropImage that takes 2 paramters: the main image and the area you want to cut off (crop) as Rectangle

    private static Image CropImage(Image img, Rectangle cropArea)
    {
       Bitmap bmpImage = new Bitmap(img);
       Bitmap bmpCrop = bmpImage.Clone(cropArea,
       bmpImage.PixelFormat);
       return (Image)(bmpCrop);
    }

    For full details check this link: http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing


    Ali Hamdar (alihamdar.com)
    Monday, September 13, 2010 6:36 PM
  • Monday, September 13, 2010 6:38 PM
  • http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/49174c48-dfda-4425-9638-77b0a6145b8a
    Ali Hamdar (alihamdar.com)


    I don't think this is what I need. I believe (correct me if I am wrong) that this will compress the whole image in a smaller area which is the Rectangle. The location parameter is absent in there.

     

    Thanks.


    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Monday, September 13, 2010 7:10 PM
  • No, it will not compress - it will crop. It will cut off the rectangle that you specified. The Rectangle is instantiated with x and y which represent the position from where to cut off and width and height representing what to cut off.
    Ali Hamdar (alihamdar.com)
    Monday, September 13, 2010 7:12 PM
  • No, it will not compress - it will crop. It will cut off the rectangle that you specified. The Rectangle is instantiated with x and y which represent the position from where to cut off and width and height representing what to cut off.
    Ali Hamdar (alihamdar.com)

    Ali, how about the location/position?
    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Monday, September 13, 2010 7:15 PM
  • OK, I think I got it. The rectangle is relative to the original iamge therefore it has to have the starting position, correct?

    Thanks.


    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Monday, September 13, 2010 7:17 PM
  • As I told you, it is in the rectangle instance.

    Rectangle rectangle = new Rectangle(20, 30, 50, 10);

    Here the 20 is the x and the 30 is the y which represents the locations - the width of what to crop is 50 and the height is 10;

    Then send rectangle instance as a parameter to the function.


    Ali Hamdar (alihamdar.com)
    • Proposed as answer by Figo Fei Wednesday, September 15, 2010 7:10 AM
    • Marked as answer by Figo Fei Friday, September 17, 2010 7:52 AM
    Monday, September 13, 2010 7:18 PM
  • Hi Alex,

    I'm following up with this case, have you found a way to create a sub-image and compress it?

    If the problem is solved, please let us know.

    Thanks.


    Figo Fei
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, September 15, 2010 7:18 AM
  • Hi Alex,

    I'm following up with this case, have you found a way to create a sub-image and compress it?

    If the problem is solved, please let us know.

    Thanks.


    Figo Fei
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Figo,

    Beside the thumbnail methods has the Drawing class endless overloads for this, it is just a matter of preference which you use and which aspects you want to mainly.

    If you ask for an answer on this question to the OP, than you can better throw a yuan in the air at every answer, catch it, and see what is on top. Almost all answers are correct.

    :-)


    Success
    Cor
    Wednesday, September 15, 2010 8:32 AM
  • Hi Cor,

    I'm impressed by your helpful replies and your humor alike. :-)

    As Alex is the OP, I'm following the process, trying to keep the status updated with him and just in case that he met with further problem by using the approaches.

    Thanks again for your comment.

    Have a nice day!


    Figo Fei
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, September 15, 2010 9:03 AM
  • Hi Alex,

    I'm following up with this case, have you found a way to create a sub-image and compress it?

    If the problem is solved, please let us know.

    Thanks.


    Figo Fei
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thank you Fei,

    Yes, Ali's suggestion works the best. I've marked his post as "answer." I am sure the thumbnail may work as well but cropping is what I really need.

     


    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Monday, September 20, 2010 6:50 PM
  • Hi Alex,

    I'm following up with this case, have you found a way to create a sub-image and compress it?

    If the problem is solved, please let us know.

    Thanks.


    Figo Fei
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Figo,

    Beside the thumbnail methods has the Drawing class endless overloads for this, it is just a matter of preference which you use and which aspects you want to mainly.

    If you ask for an answer on this question to the OP, than you can better throw a yuan in the air at every answer, catch it, and see what is on top. Almost all answers are correct.

    :-)


    Success
    Cor

    This is true.
    AlexB - Win_7 Pro64, SqlSer64 WinSer64
    Monday, September 20, 2010 6:51 PM