Skip to content

Commit

Permalink
feature: Refactor and centralize PosterIcon code
Browse files Browse the repository at this point in the history
Unified PosterIcon classes into a base class to avoid code duplication and simplify maintenance. Updated class for different PosterIcon variants to extend from the new PosterIconBase class.
  • Loading branch information
DineshSolanki committed Jul 26, 2024
1 parent c62d005 commit af8f445
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 173 deletions.
47 changes: 47 additions & 0 deletions FoliCon/Models/Data/PosterIconBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using NLog;
using Logger = NLog.Logger;

namespace FoliCon.Models.Data;

public abstract class PosterIconBase : UserControl
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
protected PosterIconBase()
{
}

protected PosterIconBase(object dataContext)
{
DataContext = dataContext;
}

public Bitmap RenderToBitmap()
{
return RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
}

private RenderTargetBitmap AsRenderTargetBitmap()
{
var size = new System.Windows.Size(256, 256);
Measure(size);
Arrange(new Rect(size));

var rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default);
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
rtb.Render(this);

return rtb;
}

public static Bitmap RenderTargetBitmapTo32BppArgb(BitmapSource rtb)
{
Logger.Trace("Converting RenderTargetBitmap to 32BppArgb");
var stream = new MemoryStream();
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(stream);
Logger.Trace("RenderTargetBitmap converted to 32BppArgb");
return new Bitmap(stream);
}
}
16 changes: 3 additions & 13 deletions FoliCon/Modules/Media/ProIcon.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FoliCon.Modules.utils;
using FoliCon.Models.Data;
using FoliCon.Modules.utils;
using NLog;
using Logger = NLog.Logger;

Expand All @@ -11,7 +12,7 @@ public class ProIcon(string filePath)
public Bitmap RenderToBitmap()
{
Logger.Debug("Rendering icon to bitmap");
return RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
return PosterIconBase.RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
}

private BitmapSource AsRenderTargetBitmap()
Expand All @@ -21,15 +22,4 @@ private BitmapSource AsRenderTargetBitmap()
Logger.Debug("Icon resized to 256x256, filePath: {FilePath}", filePath);
return ImageUtils.LoadBitmap(icon);
}

private static Bitmap RenderTargetBitmapTo32BppArgb(BitmapSource rtb)
{
Logger.Debug("Converting RenderTargetBitmap to 32BppArgb");
var stream = new MemoryStream();
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(stream);
Logger.Debug("RenderTargetBitmap converted to 32BppArgb");
return new Bitmap(stream);
}
}
4 changes: 2 additions & 2 deletions FoliCon/Views/PosterIcon.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<UserControl x:Class="FoliCon.Views.PosterIcon"
<data:PosterIconBase x:Class="FoliCon.Views.PosterIcon"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
Expand Down Expand Up @@ -26,4 +26,4 @@
Grid.Row="2" Width="55" Height="46" Foreground="Black"/>
</Grid>
</Grid>
</UserControl>
</data:PosterIconBase>
31 changes: 1 addition & 30 deletions FoliCon/Views/PosterIcon.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,8 @@ public PosterIcon()
{
InitializeComponent();
}
public PosterIcon(object dataContext)
public PosterIcon(object dataContext) :base(dataContext)
{
DataContext = dataContext;
InitializeComponent();
}

public Bitmap RenderToBitmap()
{
return RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
}

private RenderTargetBitmap AsRenderTargetBitmap()
{
var size = new System.Windows.Size(256, 256);
Measure(size);
Arrange(new Rect(size));

var rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default);
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
rtb.Render(this);

return rtb;
}

private static Bitmap RenderTargetBitmapTo32BppArgb(BitmapSource rtb)
{
var stream = new MemoryStream();
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(stream);
return new Bitmap(stream);
}
}
4 changes: 2 additions & 2 deletions FoliCon/Views/PosterIconAlt.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<UserControl x:Class="FoliCon.Views.PosterIconAlt"
<data:PosterIconBase x:Class="FoliCon.Views.PosterIconAlt"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
Expand Down Expand Up @@ -27,4 +27,4 @@
Grid.Row="2" Width="55" Height="46" Foreground="Black"/>
</Grid>
</Grid>
</UserControl>
</data:PosterIconBase>
31 changes: 1 addition & 30 deletions FoliCon/Views/PosterIconAlt.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,8 @@ public PosterIconAlt()
{
InitializeComponent();
}
public PosterIconAlt(object dataContext)
public PosterIconAlt(object dataContext) : base(dataContext)
{
DataContext = dataContext;
InitializeComponent();
}

public Bitmap RenderToBitmap()
{
return RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
}

private RenderTargetBitmap AsRenderTargetBitmap()
{
var size = new System.Windows.Size(256, 256);
Measure(size);
Arrange(new Rect(size));

var rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default);
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
rtb.Render(this);

return rtb;
}

private static Bitmap RenderTargetBitmapTo32BppArgb(BitmapSource rtb)
{
var stream = new MemoryStream();
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(stream);
return new Bitmap(stream);
}
}
4 changes: 2 additions & 2 deletions FoliCon/Views/PosterIconFaelpessoal.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<UserControl x:Class="FoliCon.Views.PosterIconFaelpessoal"
<data:PosterIconBase x:Class="FoliCon.Views.PosterIconFaelpessoal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
Expand Down Expand Up @@ -43,4 +43,4 @@
Grid.Row="2" Width="55" Height="46" Foreground="Black"/>
</Grid>
</Grid>
</UserControl>
</data:PosterIconBase>
32 changes: 2 additions & 30 deletions FoliCon/Views/PosterIconFaelpessoal.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,15 @@
/// <summary>
/// Interaction logic for PosterIconFaelpessoal.xaml
/// </summary>
public partial class PosterIconFaelpessoal : UserControl
public partial class PosterIconFaelpessoal
{
public PosterIconFaelpessoal()
{
InitializeComponent();
}

public PosterIconFaelpessoal(object dataContext)
public PosterIconFaelpessoal(object dataContext) : base(dataContext)
{
DataContext = dataContext;
InitializeComponent();
}
public Bitmap RenderToBitmap()
{
return RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
}

private RenderTargetBitmap AsRenderTargetBitmap()
{
var size = new System.Windows.Size(256, 256);
Measure(size);
Arrange(new Rect(size));

var rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default);
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
rtb.Render(this);

return rtb;
}

private static Bitmap RenderTargetBitmapTo32BppArgb(BitmapSource rtb)
{
var stream = new MemoryStream();
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(stream);
return new Bitmap(stream);
}
}
4 changes: 2 additions & 2 deletions FoliCon/Views/PosterIconFaelpessoalHorizontal.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<UserControl x:Class="FoliCon.Views.PosterIconFaelpessoalHorizontal"
<data:PosterIconBase x:Class="FoliCon.Views.PosterIconFaelpessoalHorizontal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
Expand Down Expand Up @@ -33,4 +33,4 @@
Grid.Row="2" Width="55" Height="46" Foreground="Black"/>
</Grid>
</Grid>
</UserControl>
</data:PosterIconBase>
32 changes: 2 additions & 30 deletions FoliCon/Views/PosterIconFaelpessoalHorizontal.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,15 @@
/// <summary>
/// Interaction logic for PosterIconFaelpessoalHorizontal.xaml
/// </summary>
public partial class PosterIconFaelpessoalHorizontal : UserControl
public partial class PosterIconFaelpessoalHorizontal
{
public PosterIconFaelpessoalHorizontal()
{
InitializeComponent();
}

public PosterIconFaelpessoalHorizontal(object dataContext)
public PosterIconFaelpessoalHorizontal(object dataContext) : base(dataContext)
{
DataContext = dataContext;
InitializeComponent();
}
public Bitmap RenderToBitmap()
{
return RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
}

private RenderTargetBitmap AsRenderTargetBitmap()
{
var size = new System.Windows.Size(256, 256);
Measure(size);
Arrange(new Rect(size));

var rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default);
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
rtb.Render(this);

return rtb;
}

private static Bitmap RenderTargetBitmapTo32BppArgb(BitmapSource rtb)
{
var stream = new MemoryStream();
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(stream);
return new Bitmap(stream);
}
}
4 changes: 2 additions & 2 deletions FoliCon/Views/PosterIconLiaher.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<UserControl x:Class="FoliCon.Views.PosterIconLiaher"
<data:PosterIconBase x:Class="FoliCon.Views.PosterIconLiaher"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
Expand Down Expand Up @@ -33,4 +33,4 @@
Grid.Row="2" Width="55" Height="46" Foreground="Black"/>
</Grid>
</Grid>
</UserControl>
</data:PosterIconBase>
32 changes: 2 additions & 30 deletions FoliCon/Views/PosterIconLiaher.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,15 @@
/// <summary>
/// Interaction logic for PosterIconLiaher.xaml
/// </summary>
public partial class PosterIconLiaher : UserControl
public partial class PosterIconLiaher
{
public PosterIconLiaher()
{
InitializeComponent();
}

public PosterIconLiaher(object dataContext)
public PosterIconLiaher(object dataContext) : base(dataContext)
{
DataContext = dataContext;
InitializeComponent();
}
public Bitmap RenderToBitmap()
{
return RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
}

private RenderTargetBitmap AsRenderTargetBitmap()
{
var size = new System.Windows.Size(256, 256);
Measure(size);
Arrange(new Rect(size));

var rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Pbgra32);
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
rtb.Render(this);

return rtb;
}

private static Bitmap RenderTargetBitmapTo32BppArgb(BitmapSource rtb)
{
var stream = new MemoryStream();
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(stream);
return new Bitmap(stream);
}
}

0 comments on commit af8f445

Please sign in to comment.