Im trying to cut a psecific area in image but it dosent work good why ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chocolade
    New Member
    • Aug 2010
    • 69

    Im trying to cut a psecific area in image but it dosent work good why ?

    The file logoArea is 512x512

    Now all the int variables are the area of the ogi wich is on the right bottom corner.

    newimage is the image where i want to put the logo inside so in the end when im loading newimage in paint ill see only the logo.

    But i want that newimage will be in the size of the logo only.



    I cant understand it i used GIMP to get the excat x,y and x1,y1 and the x,y is 414,422 or so and the x1,y1 is the most bottom corner wich is 512,512 thats what the mouse told me when i put it on the bottom right corner.

    but if i put 512 in y and y1 im getting error on Color logo_color = logoArea.GetPix el(x, y); or the SetPixel say it cant be less then height or something.

    its only working for me like it is now but now im getting a new 512x512 image wich is black on the right bottom corner i see the logo and also not all of the logo but i see him.

    I dont want a new 512x512 image i want to have a new image with the size of the logo with the logo only inside. I dont understadn what am i doing worng here ?


    Code:
    public static Bitmap logoArea(Bitmap logoArea)
             {
                 int image_logo_area_x = 414;
                 int image_logo_area_y = 422;
                 int image_logo_area_x1 = 60;
                 int image_logo_area_y1 = 60;
                 Bitmap newimage;
                 newimage = new Bitmap(image_logo_area_x+image_logo_area_x1, image_logo_area_y+image_logo_area_y1);
                 int x, y;
     
                for (x = image_logo_area_x; x < image_logo_area_x + image_logo_area_x1; x++)
                 {
                     for (y = image_logo_area_y; y < image_logo_area_y + image_logo_area_y1; y++)
                     {
                         Color logo_color = logoArea.GetPixel(x, y);
                         newimage.SetPixel(x, y, logo_color);
     
                    }
                 }
                 newimage.Save(@"e:\logoarea.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                 return newimage;
             }

    EDIT!!!





    I uploaded the image to there you can see it online and also save it as...to try it.

    You can see the logo small square on the right bottom corner with the cloud and sun drawing inside and the written IMS.

    I want to cut this little logo from the image and put the little box/logo in a new image with the same size of this logo. And not in a size of 512x512!
    Attached Files
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Your new image needs to be sized to the dimensions the image will be. Then you need to copy those pixels in the source image that you want into the destination image.

    So look at the code you have...
    Code:
    int image_logo_area_x = 414;
    int image_logo_area_y = 422;
    int image_logo_area_x1 = 60;
    int image_logo_area_y1 = 60;
    Bitmap newimage;
    newimage = new Bitmap(image_logo_area_x+image_logo_area_x1, image_logo_area_y+image_logo_area_y1);
    I took a look at the image in paint and for the one you supplied, the logo is 88 pixels wide by 96 pixels high. The top-left corner of the logo is at 412, 404. This means your new image, the one that holds the logo, must be 88 pixels wide by 96 pixels high. Even though your numbers are slightly different than mine, you're actually adding the location to the size. So your image width is much larger that is needed.

    Now, you need to map the source image back to the destination pixels. So your initial loop is correct, but when you write it into the destination image, you need to shift it back by the offset amount... the offset being the start location of the image in the source image. Here's some rough code...

    Code:
    int logoStartX = ...;
    int logoStartY = ...;
    int logoWidth = ...;
    int logoHeight = ...;
    Bitmap logoImage = new Bitmap(logoWidth, logoHeight);
    
    for (int x = 0; x < logoWidth; x++)
      for (int y = 0; y < logoHeight; y++)
        logoImage.SetPixel(x, y, sourceImage.GetPixel(logoStartX + x, logoStartY + y));
    (NOTE: Alternatively you could have looped from logoStartX to logoStartX + logoWidth and subtracted logoStartX from the x-coordinate when you wrote the destination pixel.)

    Now, that's the hard way and I think it's absolutely fantastic to know how to do that; however, there's an easier way. The Graphics.DrawIm age method takes a source rectangle and a destination rectangle. So you can use it to crop the source image and get the logo. You can then draw this into a new image.

    Code:
    Bitmap logoImage = new Bitmap(logoWidth, logoHeight);
    Graphics g = Graphics.FromImage(logoImage);
    g.DrawImage(
      sourceImage,
      new Rectangle(0, 0, logoWidth, logoHeight), // target rectangle
      new Rectangle(logoStartX, logoStartY, logoWidth, logoHeight), // crop rectangle
      GraphicsUnit.Pixel
    );
    Hopefully that makes sense :)

    Comment

    • Chocolade
      New Member
      • Aug 2010
      • 69

      #3
      GaryTexmo i tried something but it dosent work yet.
      Can you take a look and fix the vlaues or the variables places ?

      Code:
      public static Bitmap logoCut(Bitmap logoCut)
              {
                  int image_logo_area_x = 412;
                  int image_logo_area_y = 404;
                  int image_logo_area_x1 = 512;
                  int image_logo_area_y1 = 512;
                 
      
                  Bitmap newimage;
                  newimage = new Bitmap(logoCut.Width-image_logo_area_x,logoCut.Height-image_logo_area_y);
                  int x, y;
      
                  for (x = image_logo_area_x; x < image_logo_area_x+image_logo_area_x1; x++)
                  {
                      for (y = image_logo_area_y; y < image_logo_area_y+image_logo_area_y1; y++)
                      {
                          Color logo_color = logoCut.GetPixel(x,y);
                          newimage.SetPixel(x-image_logo_area_x, y-image_logo_area_y1, logo_color);
      
                      }
                  }
                  return newimage;
              }
      Im getting error on the SetPixel say its cant be < height


      Thanks for help.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Start plugging in numbers (you can also use debug statements if you're not the pen and paper type) and see what is actually happening.

        The error is fairly descriptive, you're trying to access a y coordinate that's higher than your image, you just need to find out why it's doing that. I'd also suggest outputting the width and height of your destination image, to verify that the dimensions are what you expect.

        Comment

        Working...