🐛 优化代码

This commit is contained in:
BookerLiu
2023-04-07 16:10:29 +08:00
parent 77a6233e63
commit 058ed5016a

View File

@@ -204,48 +204,55 @@ namespace GeekDesk.Util
try try
{ {
Image img = Image.FromFile(filePath); FileInfo file = new FileInfo(filePath);
if (img.Width <= tWidth && img.Height <= tHeight) if (file.Exists && file.Length > 0)
{ {
return GetBitmapImageByFile(filePath); Image img = Image.FromFile(filePath);
} if (img.Width <= tWidth && img.Height <= tHeight)
else
{
Bitmap loBMP = new Bitmap(filePath);
ImageFormat loFormat = loBMP.RawFormat;
decimal lnRatio;
int lnNewWidth;
int lnNewHeight;
if (loBMP.Width > loBMP.Height)
{ {
lnRatio = (decimal)tWidth / loBMP.Width; return GetBitmapImageByFile(filePath);
lnNewWidth = tWidth;
decimal lnTemp = loBMP.Height * lnRatio;
lnNewHeight = (int)lnTemp;
} }
else else
{ {
lnRatio = (decimal)tHeight / loBMP.Height; Bitmap loBMP = new Bitmap(filePath);
lnNewHeight = tHeight; ImageFormat loFormat = loBMP.RawFormat;
decimal lnTemp = loBMP.Width * lnRatio;
lnNewWidth = (int)lnTemp; decimal lnRatio;
} int lnNewWidth;
Bitmap bmpOut = new Bitmap(lnNewWidth, lnNewHeight); int lnNewHeight;
Graphics g = Graphics.FromImage(bmpOut); if (loBMP.Width > loBMP.Height)
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; {
g.FillRectangle(System.Drawing.Brushes.White, 0, 0, lnNewWidth, lnNewHeight); lnRatio = (decimal)tWidth / loBMP.Width;
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight); lnNewWidth = tWidth;
loBMP.Dispose(); decimal lnTemp = loBMP.Height * lnRatio;
string tempPath = Constants.APP_DIR + "\\temp"; lnNewHeight = (int)lnTemp;
if (File.Exists(tempPath)) }
{ else
{
lnRatio = (decimal)tHeight / loBMP.Height;
lnNewHeight = tHeight;
decimal lnTemp = loBMP.Width * lnRatio;
lnNewWidth = (int)lnTemp;
}
Bitmap bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(System.Drawing.Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
loBMP.Dispose();
string tempPath = Constants.APP_DIR + "\\temp";
if (File.Exists(tempPath))
{
File.Delete(tempPath);
}
bmpOut.Save(tempPath, loFormat);
BitmapImage bm = GetBitmapImageByFile(tempPath);
File.Delete(tempPath); File.Delete(tempPath);
return bm;
} }
bmpOut.Save(tempPath, loFormat); } else
BitmapImage bm = GetBitmapImageByFile(tempPath); {
File.Delete(tempPath); return Base64ToBitmapImage(Constants.DEFAULT_IMG_IMAGE_BASE64);
return bm;
} }
} }
catch (Exception e) catch (Exception e)
@@ -365,14 +372,18 @@ namespace GeekDesk.Util
{ {
try try
{ {
string strExt = Path.GetExtension(path).Substring(1); string ext = Path.GetExtension(path);
string suffixs = "bmp,jpg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,WMF,webp,avif"; if (!string.IsNullOrEmpty(ext))
string[] suffixArr = suffixs.Split(',');
foreach (string suffix in suffixArr)
{ {
if (suffix.Equals(strExt, StringComparison.InvariantCultureIgnoreCase)) string strExt = Path.GetExtension(path).Substring(1);
string suffixs = "bmp,jpg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,WMF,webp,avif";
string[] suffixArr = suffixs.Split(',');
foreach (string suffix in suffixArr)
{ {
return true; if (suffix.Equals(strExt, StringComparison.InvariantCultureIgnoreCase))
{
return true;
}
} }
} }
return false; return false;