Perspective image-upload C# method -
i'm working on upload function in asp.net web application need perspective picture before save it.
ex. http://i.stack.imgur.com/yam2s.png
i know can css3 , jquery on frontend, actual picture looks when gets uploaded.
i hope of guys can me c# method under picture upload.
//add new item: public struct vertex { public double x, y, z; public vertex(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } } //in page's code add: public pointf getperspectiveprojectionof(double x, double y, double z) { double size = 2.0 * z * math.tan(math.pi / 4.0); return new pointf((float)(x / size + 0.5), (float)(1.0 - y / size - 0.5)); } public image upload(image image, double distance, double spinindegrees, int widthofnewimage, int heightofnewimage) { double spininradians = spinindegrees * math.pi / 180.0; var v1 = new vertex((-image.width / 2.0) * math.cos(spininradians), image.height / 2.0, (-image.width / 2.0) * math.sin(spininradians)); var v2 = new vertex(-v1.x, v1.y, -v1.z); var v3 = new vertex(v2.x, -v2.y, v2.z); var v4 = new vertex(v1.x, -v1.y, v1.z); v1.z += distance; v2.z += distance; v3.z += distance; v4.z += distance; if (v1.z < 0.0 || v2.z < 0.0 || v3.z < 0.0 || v4.z < 0.0) { throw new argumentexception("you heading close spinned image! should increase distance.", "distance"); } image img = new bitmap(widthofnewimage, heightofnewimage); graphics graphics = graphics.fromimage(img); graphics.smoothingmode = smoothingmode.antialias; solidbrush sb = new solidbrush(color.empty); bitmap bitmap = (bitmap)image; (int y = 0; y < image.height; y++) { (int x = 0; x < image.width; x++) { var x1 = v1.x + x * math.cos(spininradians); var z1 = v1.z + x * math.sin(spininradians); var y1 = v1.y - y; var x2 = v1.x + (x + 1) * math.cos(spininradians); var z2 = v1.z + (x + 1) * math.sin(spininradians); var y2 = v1.y - y - 1; var p1 = getperspectiveprojectionof(x1, y1, z1); var p2 = getperspectiveprojectionof(x2, y1, z2); var p3 = getperspectiveprojectionof(x2, y2, z2); var p4 = getperspectiveprojectionof(x1, y2, z1); p1.x *= widthofnewimage; p1.y *= heightofnewimage; p2.x *= widthofnewimage; p2.y *= heightofnewimage; p3.x *= widthofnewimage; p3.y *= heightofnewimage; p4.x *= widthofnewimage; p4.y *= heightofnewimage; sb.color = bitmap.getpixel(x, y); graphics.fillpolygon(sb, new pointf[] { p1, p2, p3, p4 }); } } sb.dispose(); graphics.dispose(); return img; } verify are:
using system.drawing; //and using systen.drawing.drawing2d; namespaces, , of course have added reference system.drawing. can play method , give distance , angle until satisfied result. can use editors, photoshop, crop result image if necessary , can use of purposes. tested @ home before posting answer , @ last worked great me. there no reasons why shouldn't work too. hope helps , match request. have fun it!
Comments
Post a Comment