How to reduce the number of lines of code in this piece of code?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
How can I reduce code redundancy in the example below? Please respond with code. Someone at work suggested I should be use delegates. I am confused. I know I can use generics to replace method function1 but not sure how I can use delegates here.
public static byte[] function1 (WpfControl1 plot)
{
var pic = SubFunction(plot);
<pre style="font-family:Consolas; background-color:white <span style="color:blue using (<span style="color:blue var ms = <span style="color:blue new <span style="color:#2b91af MemoryStream())
{
pic.Save(ms);
<span style="color:blue byte[] bits = ms.ToArray();
<span style="color:blue return bits;
}[/code]
}
public static byte[] function1 (WpfControl2 plot)
{
var pic = SubFunction(plot);
<pre style="background-color:white; font-family:Consolas <span style="color:blue using (<span style="color:blue var ms = <span style="color:blue new <span style="color:#2b91af MemoryStream())
{
pic.Save(ms);
<span style="color:blue byte[] bits = ms.ToArray();
<span style="color:blue return bits;
}[/code]
}
public static byte[] function1 (WpfControl3 plot)
{
var pic = SubFunction(plot);
<pre style="background-color:white; font-family:Consolas <span style="color:blue using (<span style="color:blue var ms = <span style="color:blue new <span style="color:#2b91af MemoryStream())
{
pic.Save(ms);
<span style="color:blue byte[] bits = ms.ToArray();
<span style="color:blue return bits;
}[/code]
}
<pre style="font-family:Consolas; background-color:white <span style="color:blue public <span style="color:blue static <span style="color:#2b91af PngBitmapEncoder Subfunction (Wpf<span style="color:#2b91af Control1 plot)
{
System.Windows.<span style="color:#2b91af Size sizeOfControl = <span style="color:blue new System.Windows.<span style="color:#2b91af Size(plot.Width, plot.Height);
plot.Measure(sizeOfControl);
plot.Arrange(<span style="color:blue new System.Windows.<span style="color:#2b91af Rect(sizeOfControl));

<span style="color:#2b91af RenderTargetBitmap renderBitmap = <span style="color:blue new <span style="color:#2b91af RenderTargetBitmap((<span style="color:#2b91af Int32)sizeOfControl.Width, (<span style="color:#2b91af Int32)sizeOfControl.Height, 96d, 96d, <span style="color:#2b91af PixelFormats.Pbgra32);
renderBitmap.Render(plot);

<span style="color:#2b91af PngBitmapEncoder pngEncoder = <span style="color:blue new <span style="color:#2b91af PngBitmapEncoder();
pngEncoder.Frames.Add(<span style="color:#2b91af BitmapFrame.Create(renderBitmap));

<span style="color:blue return pngEncoder;
}[/code]
<pre style="background-color:white; font-family:Consolas <span style="color:blue public <span style="color:blue static <span style="color:#2b91af PngBitmapEncoder Subfunction (Wpf<span style="color:#2b91af Control2 plot)
{
System.Windows.<span style="color:#2b91af Size sizeOfControl = <span style="color:blue new System.Windows.<span style="color:#2b91af Size(plot.Width, plot.Height);
plot.Measure(sizeOfControl);
plot.Arrange(<span style="color:blue new System.Windows.<span style="color:#2b91af Rect(sizeOfControl));

<span style="color:#2b91af RenderTargetBitmap renderBitmap = <span style="color:blue new <span style="color:#2b91af RenderTargetBitmap((<span style="color:#2b91af Int32)sizeOfControl.Width, (<span style="color:#2b91af Int32)sizeOfControl.Height, 96d, 96d, <span style="color:#2b91af PixelFormats.Pbgra32);
renderBitmap.Render(plot);

<span style="color:#2b91af PngBitmapEncoder pngEncoder = <span style="color:blue new <span style="color:#2b91af PngBitmapEncoder();
pngEncoder.Frames.Add(<span style="color:#2b91af BitmapFrame.Create(renderBitmap));

<span style="color:blue return pngEncoder;
}[/code]
<pre style="background-color:white; font-family:Consolas <span style="color:blue public <span style="color:blue static <span style="color:#2b91af PngBitmapEncoder Subfunction (Wpf<span style="color:#2b91af Control3 plot)
{
System.Windows.<span style="color:#2b91af Size sizeOfControl = <span style="color:blue new System.Windows.<span style="color:#2b91af Size(plot.Width, plot.Height);
plot.Measure(sizeOfControl);
plot.Arrange(<span style="color:blue new System.Windows.<span style="color:#2b91af Rect(sizeOfControl));

<span style="color:#2b91af RenderTargetBitmap renderBitmap = <span style="color:blue new <span style="color:#2b91af RenderTargetBitmap((<span style="color:#2b91af Int32)sizeOfControl.Width, (<span style="color:#2b91af Int32)sizeOfControl.Height, 96d, 96d, <span style="color:#2b91af PixelFormats.Pbgra32);
renderBitmap.Render(plot);

<span style="color:#2b91af PngBitmapEncoder pngEncoder = <span style="color:blue new <span style="color:#2b91af PngBitmapEncoder();
pngEncoder.Frames.Add(<span style="color:#2b91af BitmapFrame.Create(renderBitmap));

<span style="color:blue return pngEncoder;
}[/code]


<
LA
<br/>

View the full article
 
Back
Top