Skip to content

Commit

Permalink
Cleanup Image_Handler Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCarbon committed Jun 29, 2024
1 parent 1674ad9 commit ce205c6
Showing 1 changed file with 21 additions and 56 deletions.
77 changes: 21 additions & 56 deletions SBRW.Launcher.Core.Theme/Image_Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,45 +27,35 @@ public static Image Grayscale(string Image_Location)
}
else
{
FileStream The_Image = default;

try
{
The_Image = File.Open(Image_Location, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
Bitmap The_Viewer = default;

try
{
The_Viewer = new Bitmap(The_Image);
return GreyScale(The_Viewer);
}
catch (Exception)
using (FileStream The_Image = File.Open(Image_Location, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
/* Error #2 */
return default;
}
finally
{
if (The_Viewer != default)
try
{
The_Viewer.Dispose();
using (Bitmap The_Viewer = new Bitmap(The_Image))
{
try
{
return GreyScale(The_Viewer);
}
catch (Exception)
{
/* Error #2 */
return default;
}
}
}
catch (Exception)
{
return default;
}
}
}
catch (Exception)
{
return default;
}
finally
{
if (The_Image != default)
{
The_Image.Close();
The_Image.Dispose();
}

GC.Collect();
}
}
}
/// <summary>
Expand All @@ -84,22 +74,16 @@ private static Bitmap GreyScale(Bitmap Image_Original)
Bitmap New_Bitmap = new Bitmap(Image_Original.Width, Image_Original.Height);

//Get a graphics object from the new image
Graphics Custom_Graphics = default;
try
using (Graphics Custom_Graphics = Graphics.FromImage(New_Bitmap))
{
Custom_Graphics = Graphics.FromImage(New_Bitmap);

//Create the grayscale ColorMatrix
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{ new float[] {.3f, .3f, .3f, 0, 0}, new float[] {.59f, .59f, .59f, 0, 0}, new float[]
{.11f, .11f, .11f, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {0, 0, 0, 0, 1} });

//create some image attributes
ImageAttributes Image_Attributes = default;

try
using (ImageAttributes Image_Attributes = new ImageAttributes())
{
Image_Attributes = new ImageAttributes();
//set the color matrix attribute
Image_Attributes.SetColorMatrix(colorMatrix);

Expand All @@ -108,21 +92,6 @@ private static Bitmap GreyScale(Bitmap Image_Original)
Custom_Graphics.DrawImage(Image_Original, new Rectangle(0, 0, Image_Original.Width, Image_Original.Height),
0, 0, Image_Original.Width, Image_Original.Height, GraphicsUnit.Pixel, Image_Attributes);
}
finally
{
if (Image_Attributes != default)
{
Image_Attributes.Dispose();
}
}
}
finally
{
if (Custom_Graphics != default)
{
//Dispose the Graphics object
Custom_Graphics.Dispose();
}
}

return New_Bitmap;
Expand All @@ -131,10 +100,6 @@ private static Bitmap GreyScale(Bitmap Image_Original)
{
return default;
}
finally
{
GC.Collect();
}
}
else
{
Expand All @@ -147,6 +112,6 @@ private static Bitmap GreyScale(Bitmap Image_Original)
/// Image loading toolset class which corrects the bug that prevents paletted PNG images with transparency from being loaded as paletted.
/// </summary>
/// <remarks><i><b>Supported only on .NET-Windows and .NET Frameworks</b></i></remarks>
public class Bitmap_Handler { }
public class Image_Handler { }
#endif
}

0 comments on commit ce205c6

Please sign in to comment.