capture part of a bitmap
- 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()
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.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);
can any one help?
BadButBit
my code is perfect until i don't find a bug
Risposte
- 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- Contrassegnato come rispostaBadButBit domenica 8 novembre 2009 7.31
Tutte le risposte
- 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- Contrassegnato come rispostaBadButBit domenica 8 novembre 2009 7.31
- 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 Bitmap cropBitmap(Bitmap originalBitmap, Rectangle cropRectangle) { return originalBitmap.Clone(cropRectangle,originalBitmap.PixelFormat); }

