styx
styx

Reputation: 1915

split image to multiple image from the bottom up

I have a picture I want to print, but it's too big for one page so i have decided to split it into multiple images i have tried a method, but now im using this (Talha Irfan answer) i also tried the other solutions there but those didnt worked as well (ex. bm.Clone(rec, bm.PixelFormat);)

and here's my code(this is on non-form class)

  Bitmap bm = new Bitmap(frmPrint.Width, frmPrint.Height);
  Rectangle rec = new Rectangle(0, 200, 576, 300); 
  Bitmap bitmap = cropImg(bm, rec);   

 frmPrint.DrawToBitmap(bitmap, rec);
 frmPrint._img = bitmap;
 frmPrint.setImage();

and setImage function(on some form)

  public void setImage()
  {
      pictureBox3.BackgroundImage = _img;       
      this.ShowDialog();
  }

and cropImg is the same as cropAtRect

the below shows the original image (on the left) the wanted result in the blue rectangle and the actual result on the right

PS my actual image size is (height = 698, wifht = 576)

Edit - as suggested below

on non-form class

 Rectangle cropRect = new Rectangle(0, 0, 576, 698); 
  Bitmap target = new Bitmap(cropRect.Width, cropRect.Height, bm.PixelFormat);
  frmPrint.setImage(bm, target, cropRect);
  target.Dispose();

at form class

  public void setImage(Bitmap src, Bitmap target, Rectangle cropRect)
    {


        pictureBox3.Visible = false;
        using (Graphics g = Graphics.FromImage(target))
        {
            g.DrawImage(src, new Rectangle(pictureBox3.Location.X, pictureBox3.Location.Y, target.Width, target.Height),
                             cropRect,
                             GraphicsUnit.Pixel);
        }      
        this.ShowDialog();
    }

enter image description here

Upvotes: 0

Views: 547

Answers (1)

TaW
TaW

Reputation: 54433

Control.DrawToBitmap will always try to draw the whole control or form and will always start at the top. The parameter:

targetBounds Type: System.Drawing.Rectangle

The bounds within which the control is rendered.

as the name implies, sets the target, not the source rectangle. Hence the white space above your result.

Move the line before cropping with a rectangle that holds the full area, maybe like this:

DrawToBitmap(bm, ClientRectangle);

and then crop the lower part as before..

Note that the cropping trick from your link will not work for DrawToBitmap; using a rectangle with a negative offset will cause a parameter exception.


Btw: to safely dispose of a Bitmap in a PictureBox use this:

Bitmap dummy = (Bitmap )somePictureBox.Image;
somePictureBox.Image = null;
if (dummy != null) dummy.Dispose;

And, indeed, the answer by ChrisJJ in the link leaks the Graphics object.


Update:

Since you seem to have lost control over the various changes and suggestions, here is the minimal code change from the original post:

Bitmap bm = new Bitmap(frmPrint.ClientWidth, frmPrint.ClientHeight);
DrawToBitmap(bm, frmPrint.ClientRectangle);

Rectangle rec = new Rectangle(0, 200, 576, 300); 
Bitmap bitmap = cropImg(bm, rec);   

frmPrint._img = bitmap;
frmPrint.setImage();

With:

public void setImage()
{
   Bitmap dummy = pictureBox3.BackgroundImage;
   pictureBox3.BackgroundImage = null;
   if (dummy != bnull) dummy.Dispose();
   pictureBox3.BackgroundImage = _img;       
   this.ShowDialog();
}

In the cropImg function add a g.Dispose before returning.

Upvotes: 1

Related Questions