질문하기질문하기
 

답변됨capture part of a bitmap

  • 2009년 11월 8일 일요일 오전 5:37BadButBit 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     코드 있음
    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

답변

  • 2009년 11월 8일 일요일 오전 5:46Tamer OzMVP사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨코드 있음
    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
    
    • 답변으로 표시됨BadButBit 2009년 11월 8일 일요일 오전 7:31
    •  

모든 응답

  • 2009년 11월 8일 일요일 오전 5:46Tamer OzMVP사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨코드 있음
    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
    
    • 답변으로 표시됨BadButBit 2009년 11월 8일 일요일 오전 7:31
    •  
  • 2009년 11월 8일 일요일 오전 7:32BadButBit 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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
  • 2009년 11월 8일 일요일 오전 7:35JohnWein 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     코드 있음
        Bitmap cropBitmap(Bitmap originalBitmap, Rectangle cropRectangle)
        {
          return originalBitmap.Clone(cropRectangle,originalBitmap.PixelFormat);
        }