Skip to content

Commit

Permalink
Abandoned the effort to presevre the alpha channel.
Browse files Browse the repository at this point in the history
Updated and hardened code
Changed the display configuration for my computer
  • Loading branch information
ScottyMac52 committed Aug 11, 2019
1 parent 0f550af commit c61f3fb
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 87 deletions.
88 changes: 72 additions & 16 deletions MFDSettingsManager/Models/ConfigurationModelBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using log4net;
using MFDSettingsManager.Extensions;
using System;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Media;
Expand Down Expand Up @@ -124,30 +125,47 @@ protected virtual string GetReadableString()
/// Crop the specified image
/// </summary>
/// <param name="imagePath"></param>
/// <param name="useNewMethod">If true then the NEW transparency preserving crop and resize are used</param>
/// <returns></returns>
public virtual BitmapSource CropImage(string imagePath)
public virtual BitmapSource CropImage(string imagePath, bool useNewMethod = false)
{
if (Enabled)
{
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(imagePath, UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();

var imgSource = new Uri(imagePath, UriKind.RelativeOrAbsolute);
Int32Rect offSet = new Int32Rect(XOffsetStart, YOffsetStart, XOffsetFinish - XOffsetStart, YOffsetFinish - YOffsetStart);
CroppedBitmap croppedBitmap = new CroppedBitmap(src, offSet);
var noAlphaSource = new FormatConvertedBitmap();
noAlphaSource.BeginInit();
noAlphaSource.Source = croppedBitmap;
noAlphaSource.DestinationFormat = PixelFormats.Bgr24;
noAlphaSource.AlphaThreshold = 0;
noAlphaSource.EndInit();
SaveImage(noAlphaSource, CacheFolder, $"X_{XOffsetStart}To{XOffsetFinish}Y_{YOffsetStart}To{YOffsetFinish}_{Opacity}");
return noAlphaSource;
if (useNewMethod)
{
var targetImage = Image.FromFile(imgSource.LocalPath);
var reSizedImage = Resize(targetImage, new System.Drawing.Size(200, 200));
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = imgSource;
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
SaveImage(src, CacheFolder, $"TEST_X_{XOffsetStart}To{XOffsetFinish}Y_{YOffsetStart}To{YOffsetFinish}_{Opacity}");
return src;
}
else
{
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = imgSource;
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
var croppedBitmap = new CroppedBitmap(src, offSet);
var noAlphaSource = new FormatConvertedBitmap();
noAlphaSource.BeginInit();
noAlphaSource.Source = croppedBitmap;
noAlphaSource.DestinationFormat = PixelFormats.Bgr24;
//noAlphaSource.AlphaThreshold = 0;
noAlphaSource.EndInit();
SaveImage(noAlphaSource, CacheFolder, $"X_{XOffsetStart}To{XOffsetFinish}Y_{YOffsetStart}To{YOffsetFinish}_{Opacity}");
return noAlphaSource;
}
}
else
{
Logger?.Warn($"The configuration is disabled, {GetReadableString()}");
return null;
}
}
Expand Down Expand Up @@ -216,6 +234,44 @@ private string GetCacheFolderForModule()
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), $"Vyper Industries\\MFD4CTS\\cache\\{ModuleName}");
}

private Image MakeImage(byte[] byteArrayIn)
{
var ms = new MemoryStream(byteArrayIn);
var returnImage = Image.FromStream(ms);
return returnImage;
}

private static Image Resize(Image image,
System.Drawing.Size size, bool preserveAspectRatio = true)
{
int newWidth;
int newHeight;
if (preserveAspectRatio)
{
var originalWidth = image.Width;
var originalHeight = image.Height;
var percentWidth = (float)size.Width / (float)originalWidth;
var percentHeight = (float)size.Height / (float)originalHeight;
var percent = percentHeight < percentWidth ? percentHeight : percentWidth;
newWidth = (int)(originalWidth * percent);
newHeight = (int)(originalHeight * percent);
}
else
{
newWidth = size.Width;
newHeight = size.Height;
}
Image newImage = new Bitmap(newWidth, newHeight);
using (Graphics graphicsHandle = Graphics.FromImage(newImage))
{
graphicsHandle.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphicsHandle.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
}
return newImage;
}

#endregion Private helpers
}
}
4 changes: 2 additions & 2 deletions MFDSettingsManager/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.6.1.0")]
[assembly: AssemblyFileVersion("2.6.1.0")]
[assembly: AssemblyVersion("2.6.2.0")]
[assembly: AssemblyFileVersion("2.6.2.0")]
4 changes: 1 addition & 3 deletions MFDisplay/AuxWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ public void LoadImage()
Close();
}
IsWindowLoaded = true;
Visibility = Visibility.Visible;
}
}

Expand Down Expand Up @@ -186,13 +185,12 @@ private BitmapSource GetBitMapSource<T>(T configSource, string cacheFolder, stri
Directory.CreateDirectory(cacheFolder);
}
filePath = Path.Combine(FilePath, configSource.FileName);
bitmapSource = configSource.CropImage(filePath);
bitmapSource = configSource.CropImage(filePath, false);
}
}
catch (Exception ex)
{
Logger?.Error($"Unable to load {configSource.ToReadableString()}.", ex);
Close();
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion MFDisplay/MFDisplay.csproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ErrorReportUrlHistory />
<FallbackCulture>en-US</FallbackCulture>
<VerifyUploadedFiles>false</VerifyUploadedFiles>
<ProjectView>ShowAllFiles</ProjectView>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
<PropertyGroup>
<EnableSecurityDebugging>false</EnableSecurityDebugging>
Expand Down
3 changes: 2 additions & 1 deletion MFDisplay/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public void CreateWindows()
if (newAuxWindow.IsWindowLoaded)
{
WindowList.Add(config.Name, newAuxWindow);
newAuxWindow.Visibility = Visibility.Visible;
}
else
{
Expand Down Expand Up @@ -267,7 +268,7 @@ private void ClearCache_Click(object sender, RoutedEventArgs e)
{
File.Delete(file.FullName);
}
catch (System.Exception ex)
catch (Exception)
{
Logger.Warn($"Unable to delete cache file: {file.FullName}");
}
Expand Down
4 changes: 2 additions & 2 deletions MFDisplay/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.6.1.0")]
[assembly: AssemblyFileVersion("2.6.1.0")]
[assembly: AssemblyVersion("2.6.2.0")]
[assembly: AssemblyFileVersion("2.6.2.0")]
Loading

0 comments on commit c61f3fb

Please sign in to comment.