Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

DrawImage resize to target size c#

private void resizeImage(string path, string originalFilename, 
                     /* note changed names */
                     int canvasWidth, int canvasHeight, 
                     /* new */
                     int originalWidth, int originalHeight)
{
    Image image = Image.FromFile(path + originalFilename);

    System.Drawing.Image thumbnail = 
        new Bitmap(canvasWidth, canvasHeight); // changed parm names
    System.Drawing.Graphics graphic = 
                 System.Drawing.Graphics.FromImage(thumbnail);

    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphic.SmoothingMode = SmoothingMode.HighQuality;
    graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
    graphic.CompositingQuality = CompositingQuality.HighQuality;

    /* ------------------ new code --------------- */

    // Figure out the ratio
    double ratioX = (double) canvasWidth / (double) originalWidth;
    double ratioY = (double) canvasHeight / (double) originalHeight;
    // use whichever multiplier is smaller
    double ratio = ratioX < ratioY ? ratioX : ratioY;

    // now we can get the new height and width
    int newHeight = Convert.ToInt32(originalHeight * ratio);
    int newWidth = Convert.ToInt32(originalWidth * ratio);

    // Now calculate the X,Y position of the upper-left corner 
    // (one of these will always be zero)
    int posX = Convert.ToInt32((canvasWidth - (originalWidth * ratio)) / 2);
    int posY = Convert.ToInt32((canvasHeight - (originalHeight * ratio)) / 2);

    graphic.Clear(Color.White); // white padding
    graphic.DrawImage(image, posX, posY, newWidth, newHeight);

    /* ------------- end new code ---------------- */

    System.Drawing.Imaging.ImageCodecInfo[] info =
                     ImageCodecInfo.GetImageEncoders();
    EncoderParameters encoderParameters;
    encoderParameters = new EncoderParameters(1);
    encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,
                     100L);            
    thumbnail.Save(path + newWidth + "." + originalFilename, info[1], 
                     encoderParameters);
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: How to scroll to bottom of ListBox 
Csharp :: process run teamviewer address parametr c# 
Csharp :: c# download image from url 
Csharp :: 110771 
Csharp :: parsing object from text file c# 
Csharp :: c# replace characters in string that are invalid using regex 
Csharp :: Unity search all chidren of parent object 
Csharp :: netmath hack console 
Csharp :: .net check connection 
Csharp :: runner dotnet trace inside docker container 
Csharp :: ascx access parent master page 
Csharp :: closing main window after clicking on a button that opens another window in wpf 
Csharp :: ms transform 
Csharp :: flutter failed asertion 
Csharp :: slider script unity 
Csharp :: visual studio private field underscore 
Csharp :: save checkbox value to database c# 
Csharp :: split nullable in c# 
Csharp :: use & symbole in xml as a text using c# 
Csharp :: c# how to group console output into columns 
Csharp :: blazor OnInitializedAsync Unhandled exception rendering component: Cannot wait on monitors on this runtime. 
Csharp :: Fibonacci Ienumerable 
Csharp :: how to add a round image unity 
Csharp :: c# condition and 
Csharp :: entity framework dynamic search 
Csharp :: model showing in scne view but not in game view 
Csharp :: c# get count from unknown list 
Csharp :: appodeal unity integration 
Csharp :: c# stack 
Csharp :: c# convert datatable to csv 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =