Find original Point within image after rotation c# -
below picture illustrates trying do.
i rotating image in c# (the white rectangle). problem need place green square (another image) on same point relative rectangle regardless of rotation. green square cannot rotated rectangle.
as can see after rotation of white rectangle end different size canvas each time.
i trying find exact point place green square after new rotated image created.
ok below code rotates image , puts dot in correct place. issue if apply transformations in rotateimage method see of rectagle red dot in wrong spot. should stress need know point of dot not place in right spot.
public class iconcontroller : controller { // // get: /icon/ public actionresult index() { return view(); } ////icon/icon?connected=true&heading=320&type=logo45 public actionresult icon(bool connected, float heading, string type) { var dir = server.mappath("/images"); //red square im trying place on blue rectangle. var path = path.combine(dir, "mapicons/center.png"); //green rectangle fixed yellow (actual center) , blue (point im trying find) var path2 = path.combine(dir, "mapicons/connected-marker.png"); image innericon = image.fromfile(path); image marker = image.fromfile(path2); using (marker) { point orginalcenter = new point((marker.width / 2), (marker.height / 2)); bitmap markerbitmap = rotateimage(new bitmap(marker), heading); marker = (image)markerbitmap; using (var bitmap = new bitmap(marker.width, marker.height)) { using (var canvas = graphics.fromimage(bitmap)) { pointf newcenter = rotatepoint(orginalcenter, 80, 120, heading, marker.width, marker.height); canvas.drawrectangle(new pen(color.black), 0, 0, bitmap.width, bitmap.height); canvas.interpolationmode = interpolationmode.highqualitybicubic; canvas.drawimage(marker, new rectangle(0, 0, marker.width, marker.height), new rectangle(0, 0, marker.width, marker.height), graphicsunit.pixel); canvas.drawimage(innericon, newcenter.x - (innericon.width / 2), newcenter.y - (innericon.height / 2)); canvas.save(); } try { bitmap.save(path.combine(dir, "result.png"), imageformat.png); path = path.combine(dir, "result.png"); } catch (exception ex) { } } } return base.file(path, "image/png"); } public static bitmap rotateimage(bitmap b, float angle) { // original bitmap needs drawn onto new bitmap bigger // because corners of original move outside original rectangle. // easy way (ok 'brute force') calculate new bounding box calculate positions of // corners after rotation , difference between maximum , minimum x , y coordinates. float wover2 = b.width / 2.0f; float hover2 = b.height / 2.0f; float radians = -(float)(angle / 180.0 * math.pi); // coordinates of corners, taking origin centre of bitmap. pointf[] corners = new pointf[]{ new pointf(-wover2, -hover2), new pointf(+wover2, -hover2), new pointf(+wover2, +hover2), new pointf(-wover2, +hover2) }; (int = 0; < 4; i++) { pointf p = corners[i]; pointf newp = new pointf((float)(p.x * math.cos(radians) - p.y * math.sin(radians)), (float)(p.x * math.sin(radians) + p.y * math.cos(radians))); corners[i] = newp; } // find min , max x , y coordinates. float minx = corners[0].x; float maxx = minx; float miny = corners[0].y; float maxy = miny; (int = 1; < 4; i++) { pointf p = corners[i]; minx = math.min(minx, p.x); maxx = math.max(maxx, p.x); miny = math.min(miny, p.y); maxy = math.max(maxy, p.y); } // size of new bitmap. sizef newsize = new sizef(maxx - minx, maxy - miny); // ...and create it. bitmap returnbitmap = new bitmap((int)math.ceiling(newsize.width), (int)math.ceiling(newsize.height)); // draw old bitmap on it. using (graphics g = graphics.fromimage(returnbitmap)) { g.translatetransform(newsize.width / 2.0f, newsize.height / 2.0f); g.rotatetransform(angle); g.translatetransform(-b.width / 2.0f, -b.height / 2.0f); g.drawimage(b, 0, 0); } return returnbitmap; } public static point rotatepoint(point pointtorotate, point centerpoint, double angleindegrees) { double angleinradians = angleindegrees * (math.pi / 180); double costheta = math.cos(angleinradians); double sintheta = math.sin(angleinradians); point pt = new point(); pt.x = (int)(costheta * (pointtorotate.x-centerpoint.x) - sintheta * (pointtorotate.y-centerpoint.y) + centerpoint.x); pt.y = (int)(sintheta * (pointtorotate.x - centerpoint.x) + costheta * (pointtorotate.y - centerpoint.y) + centerpoint.y); //p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy return pt; } }
so need able uncomment translatetransform in rotateimage method see correct rectangle , modify rotatepoint method correct new location
here working controller.
public class iconcontroller : controller { // // get: /icon/
public actionresult index() { return view(); } ////icon/icon?connected=true&heading=320&type=logo45 public actionresult icon(bool connected, float heading, string type) { var dir = server.mappath("/images"); //red square im trying place on blue rectangle. var path = path.combine(dir, "mapicons/center.png"); //green rectangle fixed yellow (actual center) , blue (point im trying find) var path2 = path.combine(dir, "mapicons/connected-marker.png"); image innericon = image.fromfile(path); image marker = image.fromfile(path2); using (marker) { point orginalcenter = new point((marker.width / 2), (marker.height / 2)); bitmap markerbitmap = rotateimage(new bitmap(marker), heading); marker = (image)markerbitmap; using (var bitmap = new bitmap(marker.width, marker.height)) { using (var canvas = graphics.fromimage(bitmap)) { pointf newcenter = rotatepoint(orginalcenter, 80, 120, heading, marker.width, marker.height); canvas.drawrectangle(new pen(color.black), 0, 0, bitmap.width, bitmap.height); canvas.interpolationmode = interpolationmode.highqualitybicubic; canvas.drawimage(marker, new rectangle(0, 0, marker.width, marker.height), new rectangle(0, 0, marker.width, marker.height), graphicsunit.pixel); canvas.drawimage(innericon, newcenter.x - (innericon.width / 2), newcenter.y - (innericon.height / 2)); canvas.save(); } try { bitmap.save(path.combine(dir, "result.png"), imageformat.png); path = path.combine(dir, "result.png"); } catch (exception ex) { } } } return base.file(path, "image/png"); } public static bitmap rotateimage(bitmap b, float angle) { // original bitmap needs drawn onto new bitmap bigger // because corners of original move outside original rectangle. // easy way (ok 'brute force') calculate new bounding box calculate positions of // corners after rotation , difference between maximum , minimum x , y coordinates. float wover2 = b.width / 2.0f; float hover2 = b.height / 2.0f; float radians = -(float)(angle / 180.0 * math.pi); // coordinates of corners, taking origin centre of bitmap. pointf[] corners = new pointf[]{ new pointf(-wover2, -hover2), new pointf(+wover2, -hover2), new pointf(+wover2, +hover2), new pointf(-wover2, +hover2) }; (int = 0; < 4; i++) { pointf p = corners[i]; pointf newp = new pointf((float)(p.x * math.cos(radians) - p.y * math.sin(radians)), (float)(p.x * math.sin(radians) + p.y * math.cos(radians))); corners[i] = newp; } // find min , max x , y coordinates. float minx = corners[0].x; float maxx = minx; float miny = corners[0].y; float maxy = miny; (int = 1; < 4; i++) { pointf p = corners[i]; minx = math.min(minx, p.x); maxx = math.max(maxx, p.x); miny = math.min(miny, p.y); maxy = math.max(maxy, p.y); } // size of new bitmap. sizef newsize = new sizef(maxx - minx, maxy - miny); // ...and create it. bitmap returnbitmap = new bitmap((int)math.ceiling(newsize.width), (int)math.ceiling(newsize.height)); // draw old bitmap on it. using (graphics g = graphics.fromimage(returnbitmap)) { g.translatetransform(newsize.width / 2.0f, newsize.height / 2.0f); g.rotatetransform(angle); g.translatetransform(-b.width / 2.0f, -b.height / 2.0f); g.drawimage(b, 0, 0); } return returnbitmap; } public static point rotatepoint(point pointtorotate, point centerpoint, double angleindegrees) { double angleinradians = angleindegrees * (math.pi / 180); double costheta = math.cos(angleinradians); double sintheta = math.sin(angleinradians); point pt = new point(); pt.x = (int)(costheta * (pointtorotate.x-centerpoint.x) - sintheta * (pointtorotate.y-centerpoint.y) + centerpoint.x); pt.y = (int)(sintheta * (pointtorotate.x - centerpoint.x) + costheta * (pointtorotate.y - centerpoint.y) + centerpoint.y); //p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy return pt; } }
Comments
Post a Comment