The following code in C # converts an image file of png, bmp into an Internet usual image under asp.net Core 2 MVC
File with .jpg formats
if (sExtension != "jpg") { //----< IsNot_Jpg >----
//< convert > string old_File = new_FullFilename_on_Server; image_webname = "Image_" + IDNote + "_" + last_ImageNr + ".jpg"; new_FullFilename_on_Server = path_for_Uploaded_Files + "\\" + image_webname;
Image_Methods.Image_resize_Length(old_File, new_FullFilename_on_Server, 1920);
if (System.IO.File.Exists(old_File)) { System.IO.File.Delete(old_File); } //</ convert > //----</ IsNot_Jpg >---- } else { Image_Methods.Image_resize_Length(new_FullFilename_on_Server, new_FullFilename_on_Server, 1920); }
|
Image Resize Method
public static void Image_resize_Length(string input_Image_Path, string output_Image_Path, int setLength, bool if_Larger_SetOriginal=true) { //---------------< Image_resize() >--------------- //*Resizes an Image in Asp.Net MVC Core 2
const long quality = 50L; Bitmap source_Bitmap = new Bitmap(input_Image_Path);
double dblWidth_origial = source_Bitmap.Width; double dblHeigth_origial = source_Bitmap.Height;
//< check orientation > bool IsOrientation_Horizontal = (dblWidth_origial > dblHeigth_origial) ? true : false; //</ check orientation >
int new_Height = 0; int new_Width = 0; double zoom_factor = 1; if (IsOrientation_Horizontal == true) { new_Width = setLength; zoom_factor = new_Width / dblWidth_origial; new_Height = (int)(dblHeigth_origial * zoom_factor); } else { new_Height = setLength; zoom_factor = new_Height / dblHeigth_origial; new_Width = (int)(dblWidth_origial * zoom_factor); }
if (zoom_factor < 1) { //----< ReSize_Only_to_Smaller >---- //< create Empty Drawarea > var new_DrawArea = new Bitmap(new_Width, new_Height); //</ create Empty Drawarea >
using (var graphic_of_DrawArea = Graphics.FromImage(new_DrawArea)) { //< setup > graphic_of_DrawArea.CompositingQuality = CompositingQuality.HighSpeed; graphic_of_DrawArea.InterpolationMode = InterpolationMode.HighQualityBicubic; graphic_of_DrawArea.CompositingMode = CompositingMode.SourceCopy; //</ setup >
//< draw into placeholder > //*imports the image into the drawarea graphic_of_DrawArea.DrawImage(source_Bitmap, 0, 0, new_Width, new_Height); //</ draw into placeholder >
//--< Output as .Jpg >-- using (var output = System.IO.File.Open(output_Image_Path + "#", FileMode.Create)) { //< setup jpg > var qualityParamId = System.Drawing.Imaging.Encoder.Quality; var encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(qualityParamId, quality); //</ setup jpg >
//< save Bitmap as Jpg > var codec = ImageCodecInfo.GetImageDecoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid); new_DrawArea.Save(output, codec, encoderParameters); //resized_Bitmap.Dispose(); output.Close(); //</ save Bitmap as Jpg > } //--</ Output as .Jpg >-- graphic_of_DrawArea.Dispose(); } source_Bitmap.Dispose();
//< swap target > if (System.IO.File.Exists(output_Image_Path)) { System.IO.File.Delete(output_Image_Path); } System.IO.File.Move(output_Image_Path + "#", output_Image_Path); //</ swap target >
//----</ ReSize_Only_to_Smaller >---- } else { //< to_Larger_Size > if(if_Larger_SetOriginal==false) { //*nothing to do source_Bitmap.Dispose(); //release handle } else { source_Bitmap.Dispose(); //release handle //< original to target > if(input_Image_Path!=output_Image_Path) { //< save:input!=output > if (System.IO.File.Exists(output_Image_Path)) { System.IO.File.Delete(output_Image_Path); } System.IO.File.Copy(input_Image_Path, output_Image_Path); //</ save:input!=output > } else { //< save:input==output > string temp_output_Image_Path = output_Image_Path + "#"; if (System.IO.File.Exists(temp_output_Image_Path)) { System.IO.File.Delete(temp_output_Image_Path); } System.IO.File.Copy(input_Image_Path, temp_output_Image_Path); System.IO.File.Delete(output_Image_Path); System.IO.File.Copy(temp_output_Image_Path, output_Image_Path); //</ save:input==output > }
//</ original to target > } //</ to_Larger_Size > } //---------------</ Image_resize() >--------------- }
|