Generelle Ausnahmenanzeige
This commit is contained in:
@@ -12,122 +12,115 @@ namespace ChatController.Utilities
|
||||
{
|
||||
public static string ScaleImage(string pFile, string pFormat, long maxUploadSize)
|
||||
{
|
||||
try
|
||||
{
|
||||
var isImageFile = Constants.ImageFileExtensions.Contains(Path.GetExtension(pFile).ToUpperInvariant());
|
||||
var isImageFile = Constants.ImageFileExtensions.Contains(Path.GetExtension(pFile).ToUpperInvariant());
|
||||
|
||||
if(!isImageFile)
|
||||
{
|
||||
return pFile;
|
||||
}
|
||||
|
||||
byte[] mediaFile;
|
||||
using(Stream reader = File.OpenRead(pFile))
|
||||
{
|
||||
mediaFile = Utils.ReadFully(reader);
|
||||
}
|
||||
|
||||
var memoryStream = new MemoryStream(mediaFile);
|
||||
var image = Image.FromStream(memoryStream);
|
||||
|
||||
float width, height;
|
||||
|
||||
if(image.Height < image.Width)
|
||||
{
|
||||
if(image.Height > 1080)
|
||||
{
|
||||
var factor = (float)image.Height / 1080;
|
||||
|
||||
height = 1080;
|
||||
|
||||
width = image.Width / factor;
|
||||
}
|
||||
else
|
||||
{
|
||||
return pFile;
|
||||
}
|
||||
}
|
||||
else if(image.Height < 1080 && image.Width < 1080)
|
||||
{
|
||||
return pFile;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(image.Width > 1080)
|
||||
{
|
||||
var factor = (float)image.Width / 1080;
|
||||
|
||||
width = 1080;
|
||||
|
||||
height = image.Height / factor;
|
||||
}
|
||||
else
|
||||
{
|
||||
return pFile;
|
||||
}
|
||||
}
|
||||
|
||||
var scaledBitmap = new Bitmap(image, new Size((int)width, (int)height));
|
||||
|
||||
const int orientationId = 0x0112;
|
||||
|
||||
if(image.PropertyIdList.Contains(orientationId))
|
||||
{
|
||||
var item = image.GetPropertyItem(orientationId);
|
||||
|
||||
scaledBitmap.SetPropertyItem(item);
|
||||
}
|
||||
|
||||
using(var graphics = Graphics.FromImage(image))
|
||||
{
|
||||
graphics.Clear(Color.Transparent);
|
||||
|
||||
graphics.InterpolationMode = InterpolationMode.Low;
|
||||
|
||||
graphics.DrawImage(scaledBitmap, (int)width, (int)height);
|
||||
}
|
||||
|
||||
// tmp_img_owch_
|
||||
var filePath = $"{Path.GetTempPath()}{Guid.NewGuid()}_tmp_img_owch{pFormat}";
|
||||
|
||||
if(pFormat.Equals(".JPG") || pFormat.Equals(".JPE") || pFormat.Equals(".JPEG") || pFormat.Equals(".BMP"))
|
||||
{
|
||||
scaledBitmap.Save(filePath, ImageFormat.Jpeg);
|
||||
}
|
||||
else if(pFormat.Equals(".PNG"))
|
||||
{
|
||||
scaledBitmap.Save(filePath, ImageFormat.Png);
|
||||
}
|
||||
else if(pFormat.Equals(".GIF"))
|
||||
{
|
||||
scaledBitmap.Save(filePath, ImageFormat.Gif);
|
||||
}
|
||||
|
||||
if(File.Exists(filePath))
|
||||
{
|
||||
var fileInfo = new FileInfo(filePath);
|
||||
var fileInfoLength = fileInfo.Length;
|
||||
|
||||
if(maxUploadSize < fileInfoLength)
|
||||
{
|
||||
File.Delete(filePath);
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
memoryStream.Dispose();
|
||||
memoryStream.Close();
|
||||
|
||||
return filePath;
|
||||
}
|
||||
catch(Exception exception)
|
||||
if(!isImageFile)
|
||||
{
|
||||
return pFile;
|
||||
}
|
||||
|
||||
byte[] mediaFile;
|
||||
using(Stream reader = File.OpenRead(pFile))
|
||||
{
|
||||
mediaFile = Utils.ReadFully(reader);
|
||||
}
|
||||
|
||||
var memoryStream = new MemoryStream(mediaFile);
|
||||
var image = Image.FromStream(memoryStream);
|
||||
|
||||
float width, height;
|
||||
|
||||
if(image.Height < image.Width)
|
||||
{
|
||||
if(image.Height > 1080)
|
||||
{
|
||||
var factor = (float)image.Height / 1080;
|
||||
|
||||
height = 1080;
|
||||
|
||||
width = image.Width / factor;
|
||||
}
|
||||
else
|
||||
{
|
||||
return pFile;
|
||||
}
|
||||
}
|
||||
else if(image.Height < 1080 && image.Width < 1080)
|
||||
{
|
||||
return pFile;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(image.Width > 1080)
|
||||
{
|
||||
var factor = (float)image.Width / 1080;
|
||||
|
||||
width = 1080;
|
||||
|
||||
height = image.Height / factor;
|
||||
}
|
||||
else
|
||||
{
|
||||
return pFile;
|
||||
}
|
||||
}
|
||||
|
||||
var scaledBitmap = new Bitmap(image, new Size((int)width, (int)height));
|
||||
|
||||
const int orientationId = 0x0112;
|
||||
|
||||
if(image.PropertyIdList.Contains(orientationId))
|
||||
{
|
||||
var item = image.GetPropertyItem(orientationId);
|
||||
|
||||
scaledBitmap.SetPropertyItem(item);
|
||||
}
|
||||
|
||||
using(var graphics = Graphics.FromImage(image))
|
||||
{
|
||||
graphics.Clear(Color.Transparent);
|
||||
|
||||
graphics.InterpolationMode = InterpolationMode.Low;
|
||||
|
||||
graphics.DrawImage(scaledBitmap, (int)width, (int)height);
|
||||
}
|
||||
|
||||
// tmp_img_owch_
|
||||
var filePath = $"{Path.GetTempPath()}{Guid.NewGuid()}_tmp_img_owch{pFormat}";
|
||||
|
||||
if(pFormat.Equals(".JPG") || pFormat.Equals(".JPE") || pFormat.Equals(".JPEG") || pFormat.Equals(".BMP"))
|
||||
{
|
||||
scaledBitmap.Save(filePath, ImageFormat.Jpeg);
|
||||
}
|
||||
else if(pFormat.Equals(".PNG"))
|
||||
{
|
||||
scaledBitmap.Save(filePath, ImageFormat.Png);
|
||||
}
|
||||
else if(pFormat.Equals(".GIF"))
|
||||
{
|
||||
scaledBitmap.Save(filePath, ImageFormat.Gif);
|
||||
}
|
||||
|
||||
if(File.Exists(filePath))
|
||||
{
|
||||
var fileInfo = new FileInfo(filePath);
|
||||
var fileInfoLength = fileInfo.Length;
|
||||
|
||||
if(maxUploadSize < fileInfoLength)
|
||||
{
|
||||
File.Delete(filePath);
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
memoryStream.Dispose();
|
||||
memoryStream.Close();
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public static bool CheckFileSize(string pathToFile, long maxFileSize)
|
||||
|
||||
@@ -213,31 +213,22 @@ namespace ChatController.Utilities
|
||||
|
||||
public static string GetAndCreateUserAppDataPath()
|
||||
{
|
||||
try
|
||||
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
||||
var companyFilePath = Path.Combine(localAppData, Resource.CompanyName);
|
||||
|
||||
if(!Directory.Exists(companyFilePath))
|
||||
{
|
||||
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
||||
var companyFilePath = Path.Combine(localAppData, Resource.CompanyName);
|
||||
|
||||
if(!Directory.Exists(companyFilePath))
|
||||
{
|
||||
Directory.CreateDirectory(companyFilePath);
|
||||
}
|
||||
|
||||
var bewoFilePath = Path.Combine(companyFilePath, Resource.ApplicationFolderName);
|
||||
|
||||
if(!Directory.Exists(bewoFilePath))
|
||||
{
|
||||
Directory.CreateDirectory(bewoFilePath);
|
||||
}
|
||||
|
||||
return bewoFilePath;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MessageBox.Show("Fehler beim Anlegen des Anwendungsordners. Sie besitzen nicht die erforderlichen Rechte, bitte wenden Sie sich an Ihren Systemadministrator.\n" + exception.Message, "BeWoPlaner", MessageBoxButton.OK, MessageBoxImage.Exclamation);
|
||||
Directory.CreateDirectory(companyFilePath);
|
||||
}
|
||||
|
||||
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||
var bewoFilePath = Path.Combine(companyFilePath, Resource.ApplicationFolderName);
|
||||
|
||||
if(!Directory.Exists(bewoFilePath))
|
||||
{
|
||||
Directory.CreateDirectory(bewoFilePath);
|
||||
}
|
||||
|
||||
return bewoFilePath;
|
||||
}
|
||||
|
||||
public static string GenerateTempName()
|
||||
@@ -253,18 +244,10 @@ namespace ChatController.Utilities
|
||||
|
||||
public static byte[] ImageToByteArray(Image pImage)
|
||||
{
|
||||
try
|
||||
using(var memoryStream = new MemoryStream())
|
||||
{
|
||||
using(var memoryStream = new MemoryStream())
|
||||
{
|
||||
pImage.Save(memoryStream, ImageFormat.Bmp);
|
||||
return memoryStream.ToArray();
|
||||
}
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MessageBox.Show("Fehler: " + exception.Message, "Fehler", MessageBoxButton.OK);
|
||||
return null;
|
||||
pImage.Save(memoryStream, ImageFormat.Bmp);
|
||||
return memoryStream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user