c# - boundaries cut the image when rotated -


i want rotate image in picture box. here code.

public static bitmap rotateimage(image image, pointf offset, float angle)         {             if (image == null)             {                 throw new argumentnullexception("image");             }             var rotatedbmp = new bitmap(image.width, image.height);             rotatedbmp.setresolution(image.horizontalresolution, image.verticalresolution);              var g = graphics.fromimage(rotatedbmp);              g.translatetransform(offset.x, offset.y);              g.rotatetransform(angle);              g.translatetransform(-offset.x, -offset.y);              g.drawimage(image, new pointf(0, 0));              return rotatedbmp;         }          private void button1_click(object sender, eventargs e)         {             image image = new bitmap(picturebox1.image);             picturebox1.image = (bitmap)image.clone();             var oldimage = picturebox1.image;             var p = new point(image.width / 2, image.height);             picturebox1.image = null;             picturebox1.image = rotateimage(image, p, 1);             picturebox1.sizemode = pictureboxsizemode.centerimage;             picturebox1.refresh();             if (oldimage != null)             {                 oldimage.dispose();             }         }          private void button2_click(object sender, eventargs e)         {             image image = new bitmap(picturebox1.image);             picturebox1.image = (bitmap)image.clone();             var oldimage = picturebox1.image;             var p = new point(image.width / 2, image.height);             picturebox1.image = null;             picturebox1.image = rotateimage(image, p, -1);             picturebox1.sizemode = pictureboxsizemode.centerimage;             picturebox1.refresh();             if (oldimage != null)             {                 oldimage.dispose();             }         } 

now problem when rotate image gets cut. here situation. enter image description here

i have stretched picture box , changed colour of form clear picture. question when have used statement

 picturebox1.image = rotateimage(image, p, 1); 

then why picture not getting right after postion same statement used situation have assign image groupbox. why not working here? have searched before of searches seem irrelevant me because use filip function rotate through 90,180,270. want rotate degree maximum upto 10 degree.

rotating controls not supported default (links talking this: link1, link2). reason why picture gets cut because, after rotation, width bigger picturebox1 one; quick solution updating size after rotation:

picturebox1.sizemode = pictureboxsizemode.autosize; //adapts size automatically 

or

picturebox1.width = image.width; picturebox1.height = image.height; 

this should acceptable solution (there has enough free space account new dimensions of image after being rotated anyway). other option affecting picturebox control directly (by affecting rectangle defining boundaries, example) more difficult.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -