C ා change picture size according to screen resolution

Keywords: C# Windows

Recently, we have encountered a problem that we need to change the size of the pictures in the program folder according to the resolution of the computer screen at this time

The following is the code implementation process:

1. Get the pictures in the folder. The name of this folder is exe, which is the same as the directory

//Read files in folder
DirectoryInfo dir = new DirectoryInfo(@"Folder name");
FileInfo[] fileInfo = dir.GetFiles();
 List<string> fileNames = new List<string>();
foreach (FileInfo item in fileInfo)
{
    fileNames.Add(item.Name);
}

2. Obtain the resolution of computer screen

//Get screen resolution under full screen
Rectangle rect = new Rectangle();
//Full screen
rect.Width = (int)System.Windows.SystemParameters.PrimaryScreenWidth;
rect.Height = (int)System.Windows.SystemParameters.PrimaryScreenHeight;
//rect = Screen.GetWorkingArea(this);//Resolution under work area, excluding taskbar
//rect.Width;//Screen width
//rect.Height;//Screen height 

3. Change image size and save

/// <summary>
/// Generate thumbnails
/// </summary>
/// <param name="serverImagePath">Picture address</param>
/// <param name="thumbnailImagePath">Thumbnail address</param>
/// <param name="width">image width</param>
/// <param name="height">Picture height</param>
/// <param name="p"></param>
public static void GetThumbnail(string serverImagePath, string thumbnailImagePath, int width, int height)
{
    System.Drawing.Image serverImage = System.Drawing.Image.FromFile(serverImagePath);
    //Sketchpad size
    int towidth = width;
    int toheight = height;
    //Pixels of thumbnail rectangle
    int ow = serverImage.Width;
    int oh = serverImage.Height;

    if (ow > oh)
    {
        toheight = serverImage.Height * width / serverImage.Width;
    }
    else
    {
        towidth = serverImage.Width * height / serverImage.Height;
    }
    //Build a new one. bmp picture
    System.Drawing.Image bm = new System.Drawing.Bitmap(width, height);
    //Create a new palette
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm);
    //Set up high quality interpolation method
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    //Set high quality,Low speed presents smoothness
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    //Empty canvas and fill with transparent background color
    g.Clear(System.Drawing.Color.White);
    //Draws the specified part of the original picture at the specified location and at the specified size
    g.DrawImage(serverImage, new System.Drawing.Rectangle((width - towidth) / 2, (height - toheight) / 2, towidth, toheight),
        0, 0, ow, oh,
        System.Drawing.GraphicsUnit.Pixel);
    try
    {
        //with jpg Format save thumbnail
        bm.Save(thumbnailImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    catch (System.Exception e)
    {
        throw e;
    }
    finally
    {
        serverImage.Dispose();
        bm.Dispose();
        g.Dispose();
    }
}

Since then, the whole function has been realized.

Posted by DJ Unique on Thu, 12 Dec 2019 09:44:33 -0800