Skip to content

Commit af8f445

Browse files
committed
feature: Refactor and centralize PosterIcon code
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.
1 parent c62d005 commit af8f445

12 files changed

+68
-173
lines changed

FoliCon/Models/Data/PosterIconBase.cs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using NLog;
2+
using Logger = NLog.Logger;
3+
4+
namespace FoliCon.Models.Data;
5+
6+
public abstract class PosterIconBase : UserControl
7+
{
8+
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
9+
protected PosterIconBase()
10+
{
11+
}
12+
13+
protected PosterIconBase(object dataContext)
14+
{
15+
DataContext = dataContext;
16+
}
17+
18+
public Bitmap RenderToBitmap()
19+
{
20+
return RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
21+
}
22+
23+
private RenderTargetBitmap AsRenderTargetBitmap()
24+
{
25+
var size = new System.Windows.Size(256, 256);
26+
Measure(size);
27+
Arrange(new Rect(size));
28+
29+
var rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default);
30+
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
31+
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
32+
rtb.Render(this);
33+
34+
return rtb;
35+
}
36+
37+
public static Bitmap RenderTargetBitmapTo32BppArgb(BitmapSource rtb)
38+
{
39+
Logger.Trace("Converting RenderTargetBitmap to 32BppArgb");
40+
var stream = new MemoryStream();
41+
BitmapEncoder encoder = new PngBitmapEncoder();
42+
encoder.Frames.Add(BitmapFrame.Create(rtb));
43+
encoder.Save(stream);
44+
Logger.Trace("RenderTargetBitmap converted to 32BppArgb");
45+
return new Bitmap(stream);
46+
}
47+
}

FoliCon/Modules/Media/ProIcon.cs

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using FoliCon.Modules.utils;
1+
using FoliCon.Models.Data;
2+
using FoliCon.Modules.utils;
23
using NLog;
34
using Logger = NLog.Logger;
45

@@ -11,7 +12,7 @@ public class ProIcon(string filePath)
1112
public Bitmap RenderToBitmap()
1213
{
1314
Logger.Debug("Rendering icon to bitmap");
14-
return RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
15+
return PosterIconBase.RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
1516
}
1617

1718
private BitmapSource AsRenderTargetBitmap()
@@ -21,15 +22,4 @@ private BitmapSource AsRenderTargetBitmap()
2122
Logger.Debug("Icon resized to 256x256, filePath: {FilePath}", filePath);
2223
return ImageUtils.LoadBitmap(icon);
2324
}
24-
25-
private static Bitmap RenderTargetBitmapTo32BppArgb(BitmapSource rtb)
26-
{
27-
Logger.Debug("Converting RenderTargetBitmap to 32BppArgb");
28-
var stream = new MemoryStream();
29-
BitmapEncoder encoder = new PngBitmapEncoder();
30-
encoder.Frames.Add(BitmapFrame.Create(rtb));
31-
encoder.Save(stream);
32-
Logger.Debug("RenderTargetBitmap converted to 32BppArgb");
33-
return new Bitmap(stream);
34-
}
3525
}

FoliCon/Views/PosterIcon.xaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<UserControl x:Class="FoliCon.Views.PosterIcon"
1+
<data:PosterIconBase x:Class="FoliCon.Views.PosterIcon"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:prism="http://prismlibrary.com/"
@@ -26,4 +26,4 @@
2626
Grid.Row="2" Width="55" Height="46" Foreground="Black"/>
2727
</Grid>
2828
</Grid>
29-
</UserControl>
29+
</data:PosterIconBase>

FoliCon/Views/PosterIcon.xaml.cs

+1-30
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,8 @@ public PosterIcon()
1010
{
1111
InitializeComponent();
1212
}
13-
public PosterIcon(object dataContext)
13+
public PosterIcon(object dataContext) :base(dataContext)
1414
{
15-
DataContext = dataContext;
1615
InitializeComponent();
1716
}
18-
19-
public Bitmap RenderToBitmap()
20-
{
21-
return RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
22-
}
23-
24-
private RenderTargetBitmap AsRenderTargetBitmap()
25-
{
26-
var size = new System.Windows.Size(256, 256);
27-
Measure(size);
28-
Arrange(new Rect(size));
29-
30-
var rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default);
31-
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
32-
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
33-
rtb.Render(this);
34-
35-
return rtb;
36-
}
37-
38-
private static Bitmap RenderTargetBitmapTo32BppArgb(BitmapSource rtb)
39-
{
40-
var stream = new MemoryStream();
41-
BitmapEncoder encoder = new PngBitmapEncoder();
42-
encoder.Frames.Add(BitmapFrame.Create(rtb));
43-
encoder.Save(stream);
44-
return new Bitmap(stream);
45-
}
4617
}

FoliCon/Views/PosterIconAlt.xaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<UserControl x:Class="FoliCon.Views.PosterIconAlt"
1+
<data:PosterIconBase x:Class="FoliCon.Views.PosterIconAlt"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:prism="http://prismlibrary.com/"
@@ -27,4 +27,4 @@
2727
Grid.Row="2" Width="55" Height="46" Foreground="Black"/>
2828
</Grid>
2929
</Grid>
30-
</UserControl>
30+
</data:PosterIconBase>

FoliCon/Views/PosterIconAlt.xaml.cs

+1-30
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,8 @@ public PosterIconAlt()
99
{
1010
InitializeComponent();
1111
}
12-
public PosterIconAlt(object dataContext)
12+
public PosterIconAlt(object dataContext) : base(dataContext)
1313
{
14-
DataContext = dataContext;
1514
InitializeComponent();
1615
}
17-
18-
public Bitmap RenderToBitmap()
19-
{
20-
return RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
21-
}
22-
23-
private RenderTargetBitmap AsRenderTargetBitmap()
24-
{
25-
var size = new System.Windows.Size(256, 256);
26-
Measure(size);
27-
Arrange(new Rect(size));
28-
29-
var rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default);
30-
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
31-
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
32-
rtb.Render(this);
33-
34-
return rtb;
35-
}
36-
37-
private static Bitmap RenderTargetBitmapTo32BppArgb(BitmapSource rtb)
38-
{
39-
var stream = new MemoryStream();
40-
BitmapEncoder encoder = new PngBitmapEncoder();
41-
encoder.Frames.Add(BitmapFrame.Create(rtb));
42-
encoder.Save(stream);
43-
return new Bitmap(stream);
44-
}
4516
}

FoliCon/Views/PosterIconFaelpessoal.xaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<UserControl x:Class="FoliCon.Views.PosterIconFaelpessoal"
1+
<data:PosterIconBase x:Class="FoliCon.Views.PosterIconFaelpessoal"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:prism="http://prismlibrary.com/"
@@ -43,4 +43,4 @@
4343
Grid.Row="2" Width="55" Height="46" Foreground="Black"/>
4444
</Grid>
4545
</Grid>
46-
</UserControl>
46+
</data:PosterIconBase>

FoliCon/Views/PosterIconFaelpessoal.xaml.cs

+2-30
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,15 @@
33
/// <summary>
44
/// Interaction logic for PosterIconFaelpessoal.xaml
55
/// </summary>
6-
public partial class PosterIconFaelpessoal : UserControl
6+
public partial class PosterIconFaelpessoal
77
{
88
public PosterIconFaelpessoal()
99
{
1010
InitializeComponent();
1111
}
1212

13-
public PosterIconFaelpessoal(object dataContext)
13+
public PosterIconFaelpessoal(object dataContext) : base(dataContext)
1414
{
15-
DataContext = dataContext;
1615
InitializeComponent();
1716
}
18-
public Bitmap RenderToBitmap()
19-
{
20-
return RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
21-
}
22-
23-
private RenderTargetBitmap AsRenderTargetBitmap()
24-
{
25-
var size = new System.Windows.Size(256, 256);
26-
Measure(size);
27-
Arrange(new Rect(size));
28-
29-
var rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default);
30-
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
31-
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
32-
rtb.Render(this);
33-
34-
return rtb;
35-
}
36-
37-
private static Bitmap RenderTargetBitmapTo32BppArgb(BitmapSource rtb)
38-
{
39-
var stream = new MemoryStream();
40-
BitmapEncoder encoder = new PngBitmapEncoder();
41-
encoder.Frames.Add(BitmapFrame.Create(rtb));
42-
encoder.Save(stream);
43-
return new Bitmap(stream);
44-
}
4517
}

FoliCon/Views/PosterIconFaelpessoalHorizontal.xaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<UserControl x:Class="FoliCon.Views.PosterIconFaelpessoalHorizontal"
1+
<data:PosterIconBase x:Class="FoliCon.Views.PosterIconFaelpessoalHorizontal"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:prism="http://prismlibrary.com/"
@@ -33,4 +33,4 @@
3333
Grid.Row="2" Width="55" Height="46" Foreground="Black"/>
3434
</Grid>
3535
</Grid>
36-
</UserControl>
36+
</data:PosterIconBase>

FoliCon/Views/PosterIconFaelpessoalHorizontal.xaml.cs

+2-30
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,15 @@
33
/// <summary>
44
/// Interaction logic for PosterIconFaelpessoalHorizontal.xaml
55
/// </summary>
6-
public partial class PosterIconFaelpessoalHorizontal : UserControl
6+
public partial class PosterIconFaelpessoalHorizontal
77
{
88
public PosterIconFaelpessoalHorizontal()
99
{
1010
InitializeComponent();
1111
}
1212

13-
public PosterIconFaelpessoalHorizontal(object dataContext)
13+
public PosterIconFaelpessoalHorizontal(object dataContext) : base(dataContext)
1414
{
15-
DataContext = dataContext;
1615
InitializeComponent();
1716
}
18-
public Bitmap RenderToBitmap()
19-
{
20-
return RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
21-
}
22-
23-
private RenderTargetBitmap AsRenderTargetBitmap()
24-
{
25-
var size = new System.Windows.Size(256, 256);
26-
Measure(size);
27-
Arrange(new Rect(size));
28-
29-
var rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default);
30-
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
31-
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
32-
rtb.Render(this);
33-
34-
return rtb;
35-
}
36-
37-
private static Bitmap RenderTargetBitmapTo32BppArgb(BitmapSource rtb)
38-
{
39-
var stream = new MemoryStream();
40-
BitmapEncoder encoder = new PngBitmapEncoder();
41-
encoder.Frames.Add(BitmapFrame.Create(rtb));
42-
encoder.Save(stream);
43-
return new Bitmap(stream);
44-
}
4517
}

FoliCon/Views/PosterIconLiaher.xaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<UserControl x:Class="FoliCon.Views.PosterIconLiaher"
1+
<data:PosterIconBase x:Class="FoliCon.Views.PosterIconLiaher"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:prism="http://prismlibrary.com/"
@@ -33,4 +33,4 @@
3333
Grid.Row="2" Width="55" Height="46" Foreground="Black"/>
3434
</Grid>
3535
</Grid>
36-
</UserControl>
36+
</data:PosterIconBase>

FoliCon/Views/PosterIconLiaher.xaml.cs

+2-30
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,15 @@
33
/// <summary>
44
/// Interaction logic for PosterIconLiaher.xaml
55
/// </summary>
6-
public partial class PosterIconLiaher : UserControl
6+
public partial class PosterIconLiaher
77
{
88
public PosterIconLiaher()
99
{
1010
InitializeComponent();
1111
}
1212

13-
public PosterIconLiaher(object dataContext)
13+
public PosterIconLiaher(object dataContext) : base(dataContext)
1414
{
15-
DataContext = dataContext;
1615
InitializeComponent();
1716
}
18-
public Bitmap RenderToBitmap()
19-
{
20-
return RenderTargetBitmapTo32BppArgb(AsRenderTargetBitmap());
21-
}
22-
23-
private RenderTargetBitmap AsRenderTargetBitmap()
24-
{
25-
var size = new System.Windows.Size(256, 256);
26-
Measure(size);
27-
Arrange(new Rect(size));
28-
29-
var rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Pbgra32);
30-
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
31-
RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
32-
rtb.Render(this);
33-
34-
return rtb;
35-
}
36-
37-
private static Bitmap RenderTargetBitmapTo32BppArgb(BitmapSource rtb)
38-
{
39-
var stream = new MemoryStream();
40-
BitmapEncoder encoder = new PngBitmapEncoder();
41-
encoder.Frames.Add(BitmapFrame.Create(rtb));
42-
encoder.Save(stream);
43-
return new Bitmap(stream);
44-
}
4517
}

0 commit comments

Comments
 (0)