Poser une questionPoser une question
 

Traitéecapture part of a bitmap

  • dimanche 8 novembre 2009 05:37BadButBit Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du code
    hi,
    i'm trying to find a way to crop a bitmap.  what i need to do is take a segment of an existing image and 'cut-n-paste' it onto a separate bitmap

    i've tried using graphics.fromimage()

    Rectangle saveArea = new Rectangle(intX * intSquareSize, intY * intSquareSize, intSquareSize, intSquareSize);
                                    bmp[intX, intY] = new Bitmap(saveArea.Width, saveArea.Height);
                                    using (Graphics g = Graphics.FromImage(bmp[intX, intY]))
                                    { g.DrawImage(bmpPic, saveArea); 
    
    but what that does is save the entire original image (bmpPic) into a new bitmap (here bmp[intx, inty]) but resized to fit this new bmp instead of cropping out the area that I would like the saveArea rectangle to specify.

    can any one help?

    BadButBit
    my code is perfect until i don't find a bug

Réponses

  • dimanche 8 novembre 2009 05:46Tamer OzMVPMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code
    Hi,

    Here is a sample.

    Hope it helps.

                Image iSource = Image.FromFile(@"c:\users\tamer\desktop\a.jpg");//load the orginal file
                int width = 100;//determine the width of the area will be copied
                int height = 100;//determine the height of the area will be copied
                Image iNew = new Bitmap(width, height);//create a new image
                Graphics g = Graphics.FromImage(iNew);//create graphics object
                Rectangle destRect = new Rectangle(0, 0, iNew.Width, iNew.Height);//create destination rectangle
                Rectangle srcRect = new Rectangle(50, 100, width, height);//get from (50,100) to (150,200). source rectangle
                g.DrawImage(iSource, destRect, srcRect, GraphicsUnit.Pixel);//draw image
                iNew.Save(@"c:\users\tamer\desktop\a2.jpg");//save new image
                g.Dispose();//dispose graphics
    
    • Marqué comme réponseBadButBit dimanche 8 novembre 2009 07:31
    •  

Toutes les réponses

  • dimanche 8 novembre 2009 05:46Tamer OzMVPMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code
    Hi,

    Here is a sample.

    Hope it helps.

                Image iSource = Image.FromFile(@"c:\users\tamer\desktop\a.jpg");//load the orginal file
                int width = 100;//determine the width of the area will be copied
                int height = 100;//determine the height of the area will be copied
                Image iNew = new Bitmap(width, height);//create a new image
                Graphics g = Graphics.FromImage(iNew);//create graphics object
                Rectangle destRect = new Rectangle(0, 0, iNew.Width, iNew.Height);//create destination rectangle
                Rectangle srcRect = new Rectangle(50, 100, width, height);//get from (50,100) to (150,200). source rectangle
                g.DrawImage(iSource, destRect, srcRect, GraphicsUnit.Pixel);//draw image
                iNew.Save(@"c:\users\tamer\desktop\a2.jpg");//save new image
                g.Dispose();//dispose graphics
    
    • Marqué comme réponseBadButBit dimanche 8 novembre 2009 07:31
    •  
  • dimanche 8 novembre 2009 07:32BadButBit Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    bif, bam, boom!

    and that solves that problem...

    thanks.  i was missing the 'GraphicsUnit.Pixel' business.

    BadButBit
    my code is perfect until i don't find a bug
  • dimanche 8 novembre 2009 07:35JohnWein Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du code
        Bitmap cropBitmap(Bitmap originalBitmap, Rectangle cropRectangle)
        {
          return originalBitmap.Clone(cropRectangle,originalBitmap.PixelFormat);
        }