extract part of a bitmap

Hi,

I have a bitmap, and would like to create a new bitmap by extracting a (rectangular) part of the existing bitmap. What is the best way to do this


Answer this question

extract part of a bitmap

  • Brandon Bloom

    not sure especially since im still learning doing the graphics stuff! I am still fascinated on how you can extract part of the image and save it...its cool

  • araymund

    I've actually just looked into this myself and this seems to work, perhaps with a bit more tinckering....



    Bitmap myExtractedPortion = new Bitmap(width, height);
    Rectangle destinationRect = new Rectangle(0, 0, width, height);
    Graphics myGraphics = Graphics.FromImage(Image)myExtractedPortion);

    myGraphics.DrawImage(Image.FromFile("file.jpg"), destinationRect, srcX, srcY, myExtractedPortion.Width, myExtractedPortion.Height, GraphicsUnit.Pixel);

    myExtractedPortion.Save("newFile.jpg", System.Drawing.Imaging.ImageFormat.Jpg);

    does this work for you I guess you need to play a bit with it to get the portion you want extracted from the original image



  • Tom Regan

    Yeah, I've thought of something like this. Just wondered if there's some cleaner way than drawing it... (specially if the source image has alpha)

  • extract part of a bitmap