You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.9 KiB

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using UnityEngine;
public class ImageUtil
{
public static Bitmap ResizeImage(Image bitmap, int maxSize)
{
float aspect = (float)bitmap.Width / bitmap.Height;
int w;
int h;
if (aspect > 1.0f)
{
w = maxSize;
h = (int)Mathf.Round(w / aspect);
}
else
{
h = maxSize;
w = (int)Mathf.Round(h * aspect);
}
return ResizeImage(bitmap, w, h);
}
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = System.Drawing.Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(System.Drawing.Drawing2D.WrapMode.Clamp);
try
{
var destRect = new Rectangle(0, 0, width, height);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}catch(Exception err)
{
Debug.Log("err:" + err.StackTrace);
}
}
}
destImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
return destImage;
}
}