c# - Resize width on upload and keep the height ratio proportional -
I am using the following system drawing code to resize the images on the upload. The problem is that the landscape or picture diagrams get distorted, because the santum drawing is making them class. Is it possible to resize only the width and keep the height proportional? And how? Thanks
http posted file imagefile = upload images.posted file; System.Drawing.Image ri = System.Drawing.Image.FromStream (imageFile.InputStream); Ri = resizebitmap ((bitmap) ri, 200, 200); Private bitmap resizebitmap (bitmap b, int nwd, inthe anhete) {bitmap result = new bitmap (nwd, hight); Using (Graphics G = Graphics.frames ((System.Drawing.Image) result) g.DrawImage (b, 0, 0, nWidth, nHeight); Return result; }
If you want to do, then a new bitmap is 200 pixels wide and made Has increased in proportion to the height ratio, you can do this:
calculate the private static integerHighthought (int olderview, int olderhite, int nudity) {If (oldwyths Or would you like to create a 200x200 bitmap with the old image scaled to fit inside, and if necessary?
Update If you are looking for an image of a fixed 200x200 size, then the image is appropriately fitted and as letterboxed It should be done:
Static RectangleF PlaceInside (Int. Old Writers, Old Hight, Int Nude, Int NewHight) {If (Old Width & old; Old Date & lt; = old} ; = 0; newWidth & lt; = 0 || newHeight & lt; = 0) New RectangleF (old width, oldhigh, nudity, newheit); Float width factor = (float) New width / (float) old width; Float height = factor = (float) newhigh / (float) oldhigh; If (widthFactor & lt; heightFactor) increases width / width; Height = widthFactor * oldHeight; // New new rectangle (x, y, width, height) return new rectangle (0, (new high-scale heitt) / 2.0 f, new watt, scale height); } Else {// Preferred height Float Scale width = heightFactor * oldWidth; / / New new rectangle (X, Y, width, height) New rectangle (NewWide - Scale wide) / 2.0 F, 0, Scaled Wide, New Highlight); }} Change the personal static bitmap size bitmap (bitmap b, int nwd, inthe anhete) {int oldWidth = b.Width; Int oldHeight = B height; Bitmap result = new bitmap (nWidth, nHeight); (Graphics G = Graphics.frimize) () (var box = PlaceInside (old width, oldhigh, nWidth, nHeight); G.DrawImage (b, box);} Return result;} Update 2 And here is a version that creates an image of width 200 and proportional height if the landscape and height 200 and the proportional width Portrait:
Resize Private Static Bitmap Size Bitmap Apto (bitmap b, int nwd, int hn, system Drawing 2.D. Interpolation mode interpolation mode) {int oldWidth = b.Width; Int oldHeight = B.High; Var Boxes = Place Inside (old place, oldhighth, evergreen, enehit); int real newview = (int) math Max (Math. Wide), 1); int real newwit = (int.) Math. (Math. Round (box. Heights), 1); Bitmap result = New bitmap (actual Newwight, Real NewWit) ; Usage (Graphics G = Graphics. Frames Ez (System.Drawing.Image) results) {g.InterpolationMode = interpolationMode; G.DrawImage (b, 0, 0, real newwyth, real nywite); } Return results; } I added a pair so that you can experiment with various properties according to Ksv3n answer.
(hopefully) last update Here is the test setup that I used to validate the code. I was able to open, resize and save various types of images on my computer. Public Static Zero TestResizeBitmapUpto (string file, string newfile) {try (var image = Bitmap.FromFile (file)) {if (image == null) return; (Bitmap b = new bitmap (image)) {(var newBitmap = ResizeBitmapUpto (b, 200, 200, using system.Drawing.Drawing2D.InterpolationMode.NearestNeighbor)) {newBitmap.Save (newFile); }}}} Hold (System.IO.FileNotFoundException e) {Debug.WriteLine (e.ToString ()); } Hold (exception e) {Debug.WriteLine (e.ToString ()); }}
Comments
Post a Comment