{x_dpi = float.Parse(ds.Tables[0].Rows[0]["XPOS"].ToString()); y_dpi = float.Parse(ds.Tables[0].Rows[0]["YPOS"].ToString()); _bmpW = Int32.Parse(ds.Tables[0].Rows[0]["WIDTH"].ToString()); _bmpH = Int32.Parse(ds.Tables[0].Rows[0]["HEIGHT"].ToString()); IMAGE_TYPE = ds.Tables[0].Rows[0]["IMAGE_TYPE"].ToString(); int bmpW = _bmpW; //New image target width int bmpH = _bmpH; Int32 newWidth = bmpW; Int32 newHeight = bmpH; HttpFileCollection files = HttpContext.Current.Request.Files; HttpPostedFile uploadfile = files["RemoteFile"]; Bitmap upBmp = (Bitmap)Bitmap.FromStream(uploadfile.InputStream); Bitmap newBmp = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); FileName = uploadfile.FileName; newBmp.SetResolution(x_dpi, y_dpi); //Get the uploaded image width and height Double upWidth = upBmp.Width; Double upHeight = upBmp.Height; int newX = 0; //Set the new top left drawing position on the image canvas int newY = 0; Double reDuce; //Keep the aspect ratio of image the same if not 4:3 and work out the newX and newY positions //to ensure the image is always in the centre of the canvas vertically and horizontally if (upWidth > upHeight) { //Landscape picture reDuce = newWidth / upWidth; //calculate the width percentage reduction as decimal newHeight = ((Int32)(upHeight * reDuce)); //reduce the uploaded image height by the reduce amount newY = ((Int32)((bmpH - newHeight) / 2)); //Position the image centrally down the canvas newX = 0; //Picture will be full width } else if (upWidth < upHeight) { //Portrait picture reDuce = newHeight / upHeight; //calculate the height percentage reduction as decimal newWidth = ((Int32)(upWidth * reDuce)); //reduce the uploaded image height by the reduce amount newX = ((Int32)((bmpW - newWidth) / 2)); //Position the image centrally across the canvas newY = 0; //Picture will be full hieght } else if (upWidth == upHeight) { //square picture reDuce = newHeight / upHeight; //calculate the height percentage reduction as decimal newWidth = ((Int32)(upWidth * reDuce)); //reduce the uploaded image height by the reduce amount newX = ((Int32)((bmpW - newWidth) / 2)); //Position the image centrally across the canvas newY = ((Int32)((bmpH - newHeight) / 2)); //Position the image centrally down the canvas } //Create a new image from the uploaded picture using the Graphics class //Clear the graphic and set the background colour to white //Use Antialias and High Quality Bicubic to maintain a good quality picture Graphics newGraphic = Graphics.FromImage(newBmp); try { //newGraphic.DrawImage(upBmp, newX, newY, newWidth, newHeight); EncoderParameters eps = new EncoderParameters(1); if (IMAGE_TYPE.ToUpper() == "JPG") { newGraphic.Clear(Color.White); newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; newGraphic.DrawImage(upBmp, newX, newY, newWidth, newHeight); newBmp.Save(Path + FileName, System.Drawing.Imaging.ImageFormat.Jpeg); } else if (IMAGE_TYPE.ToUpper() == "TIF" || IMAGE_TYPE.ToUpper() == "TIFF") { eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4); //newBmp.BitsPerSample = 1; //newBmp.SamplesPerPixel = 1; System.Drawing.Imaging.Encoder myEncoder; EncoderParameter myEncoderParameter; EncoderParameters myEncoderParameters; ImageCodecInfo myImageCodecInfo; myEncoder = System.Drawing.Imaging.Encoder.Compression; myEncoderParameters = new EncoderParameters(1); // Save the image with a color depth of 24 bits per pixel. //myEncoderParameter = // new EncoderParameter(myEncoder, 24L); myEncoderParameter = new EncoderParameter(myEncoder, (long)3); myEncoderParameters.Param[0] = myEncoderParameter; myImageCodecInfo = GetEncoderInfo("image/tiff"); upBmp.Save(Path + FileName, myImageCodecInfo, myEncoderParameters); } else if (IMAGE_TYPE.ToUpper() == "PDF") { eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4); //newBmp.BitsPerSample = 1; //newBmp.SamplesPerPixel = 1; System.Drawing.Imaging.Encoder myEncoder; EncoderParameter myEncoderParameter; EncoderParameters myEncoderParameters; ImageCodecInfo myImageCodecInfo; myEncoder = System.Drawing.Imaging.Encoder.Compression; myEncoderParameters = new EncoderParameters(1); // Save the image with a color depth of 24 bits per pixel. //myEncoderParameter = // new EncoderParameter(myEncoder, 24L); myEncoderParameter = new EncoderParameter(myEncoder, (long)3); myEncoderParameters.Param[0] = myEncoderParameter; myImageCodecInfo = GetEncoderInfo("image/tiff"); newBmp.Save(Path + FileName, myImageCodecInfo, myEncoderParameters); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)