Skip to content

Commit

Permalink
Added separate methods for PNG, JPEG, and BMP resizing. Moved private…
Browse files Browse the repository at this point in the history
… methods to a partial class. Updated to version 2.5.
  • Loading branch information
keyvan committed Apr 9, 2012
1 parent 2cb1c14 commit 9683749
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 86 deletions.
15 changes: 12 additions & 3 deletions ImageResizer4DotNet/ImageResize4DotNet.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ static void Main(string[] args)
if (!string.IsNullOrEmpty(temp))
filePath = temp;

// Low quality
// Low quality PNG
MemoryStream original = new MemoryStream(System.IO.File.ReadAllBytes(filePath));
MemoryStream resizedStreamLow = Resizer.Low(original, 400, 225);
MemoryStream resizedStreamLow = Resizer.LowPng(original, 400, 225);
File.WriteAllBytes("resizedlow.png", resizedStreamLow.ToArray());
resizedStreamLow.Position = 0;

// Low quality JPEG
original = new MemoryStream(System.IO.File.ReadAllBytes(filePath));
resizedStreamLow = Resizer.LowJpeg(original, 400, 225);
File.WriteAllBytes("resizedlow.jpg", resizedStreamLow.ToArray());

// Low quality BMP
original = new MemoryStream(System.IO.File.ReadAllBytes(filePath));
resizedStreamLow = Resizer.LowBmp(original, 400, 225);
File.WriteAllBytes("resizedlow.bmp", resizedStreamLow.ToArray());

Console.WriteLine("Done!");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="ResizerPrivate.cs" />
<Compile Include="Resizer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,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.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyVersion("2.5.0.0")]
[assembly: AssemblyFileVersion("2.5.0.0")]
134 changes: 53 additions & 81 deletions ImageResizer4DotNet/ImageResizer4DotNet/Resizer.cs
Original file line number Diff line number Diff line change
@@ -1,121 +1,93 @@
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ImageResizer4DotNet
{
public class Resizer
public partial class Resizer
{
public static MemoryStream Resize(MemoryStream original, int width, int height)
public static MemoryStream ResizePng(MemoryStream original, int width, int height)
{
MemoryStream result = new MemoryStream();
BitmapFrame newphoto = GetBitmapFrame(original, width, height, BitmapScalingMode.Unspecified);

BitmapDecoder photoDecoder = BitmapDecoder.Create(original, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
BitmapFrame photo = photoDecoder.Frames[0];

TransformedBitmap target = new TransformedBitmap(
photo,
new ScaleTransform(
width / photo.Width * 96 / photo.DpiX,
height / photo.Height * 96 / photo.DpiY,
0, 0));
BitmapFrame thumbnail = BitmapFrame.Create(target);
BitmapFrame newphoto = Resize(thumbnail, width, height, BitmapScalingMode.Unspecified);
return GetPngStream(newphoto);
}

PngBitmapEncoder targetEncoder = new PngBitmapEncoder();
targetEncoder.Frames.Add(newphoto);
targetEncoder.Save(result);
public static MemoryStream LowPng(MemoryStream original, int width, int height)
{
BitmapFrame newphoto = GetBitmapFrame(original, width, height, BitmapScalingMode.LowQuality);

return result;
return GetPngStream(newphoto);
}

public static MemoryStream Low(MemoryStream original, int width, int height)
public static MemoryStream HighPng(MemoryStream original, int width, int height)
{
MemoryStream result = new MemoryStream();
BitmapFrame newphoto = GetBitmapFrame(original, width, height, BitmapScalingMode.HighQuality);

BitmapDecoder photoDecoder = BitmapDecoder.Create(original, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
BitmapFrame photo = photoDecoder.Frames[0];

TransformedBitmap target = new TransformedBitmap(
photo,
new ScaleTransform(
width / photo.Width * 96 / photo.DpiX,
height / photo.Height * 96 / photo.DpiY,
0, 0));
BitmapFrame thumbnail = BitmapFrame.Create(target);
BitmapFrame newphoto = Resize(thumbnail, width, height, BitmapScalingMode.LowQuality);
return GetPngStream(newphoto);
}

PngBitmapEncoder targetEncoder = new PngBitmapEncoder();
targetEncoder.Frames.Add(newphoto);
targetEncoder.Save(result);
public static MemoryStream NearestNeighborPng(MemoryStream original, int width, int height)
{
BitmapFrame newphoto = GetBitmapFrame(original, width, height, BitmapScalingMode.NearestNeighbor);

return result;
return GetPngStream(newphoto);
}


public static MemoryStream High(MemoryStream original, int width, int height)
public static MemoryStream ResizeJpeg(MemoryStream original, int width, int height)
{
MemoryStream result = new MemoryStream();
BitmapFrame newphoto = GetBitmapFrame(original, width, height, BitmapScalingMode.Unspecified);

BitmapDecoder photoDecoder = BitmapDecoder.Create(original, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
BitmapFrame photo = photoDecoder.Frames[0];
return GetJpegStream(newphoto);
}

TransformedBitmap target = new TransformedBitmap(
photo,
new ScaleTransform(
width / photo.Width * 96 / photo.DpiX,
height / photo.Height * 96 / photo.DpiY,
0, 0));
BitmapFrame thumbnail = BitmapFrame.Create(target);
BitmapFrame newphoto = Resize(thumbnail, width, height, BitmapScalingMode.HighQuality);
public static MemoryStream LowJpeg(MemoryStream original, int width, int height)
{
BitmapFrame newphoto = GetBitmapFrame(original, width, height, BitmapScalingMode.LowQuality);

return GetJpegStream(newphoto);
}

PngBitmapEncoder targetEncoder = new PngBitmapEncoder();
targetEncoder.Frames.Add(newphoto);
targetEncoder.Save(result);
public static MemoryStream HighJpeg(MemoryStream original, int width, int height)
{
BitmapFrame newphoto = GetBitmapFrame(original, width, height, BitmapScalingMode.HighQuality);

return result;
return GetJpegStream(newphoto);
}

public static MemoryStream NearestNeighborJpeg(MemoryStream original, int width, int height)
{
BitmapFrame newphoto = GetBitmapFrame(original, width, height, BitmapScalingMode.NearestNeighbor);

public static MemoryStream NearestNeighbor(MemoryStream original, int width, int height)
return GetJpegStream(newphoto);
}

public static MemoryStream ResizeBmp(MemoryStream original, int width, int height)
{
MemoryStream result = new MemoryStream();
BitmapFrame newphoto = GetBitmapFrame(original, width, height, BitmapScalingMode.Unspecified);

BitmapDecoder photoDecoder = BitmapDecoder.Create(original, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
BitmapFrame photo = photoDecoder.Frames[0];
return GetBmpStream(newphoto);
}

TransformedBitmap target = new TransformedBitmap(
photo,
new ScaleTransform(
width / photo.Width * 96 / photo.DpiX,
height / photo.Height * 96 / photo.DpiY,
0, 0));
BitmapFrame thumbnail = BitmapFrame.Create(target);
BitmapFrame newphoto = Resize(thumbnail, width, height, BitmapScalingMode.NearestNeighbor);
public static MemoryStream LowBmp(MemoryStream original, int width, int height)
{
BitmapFrame newphoto = GetBitmapFrame(original, width, height, BitmapScalingMode.LowQuality);

return GetBmpStream(newphoto);
}

PngBitmapEncoder targetEncoder = new PngBitmapEncoder();
targetEncoder.Frames.Add(newphoto);
targetEncoder.Save(result);
public static MemoryStream HighBmp(MemoryStream original, int width, int height)
{
BitmapFrame newphoto = GetBitmapFrame(original, width, height, BitmapScalingMode.HighQuality);

return result;
return GetBmpStream(newphoto);
}

private static BitmapFrame Resize(BitmapFrame photo, int width, int height, BitmapScalingMode scalingMode)
public static MemoryStream NearestNeighborBmp(MemoryStream original, int width, int height)
{
DrawingGroup group = new DrawingGroup();
RenderOptions.SetBitmapScalingMode(group, scalingMode);
group.Children.Add(new ImageDrawing(photo, new Rect(0, 0, width, height)));
DrawingVisual targetVisual = new DrawingVisual();
DrawingContext targetContext = targetVisual.RenderOpen();
targetContext.DrawDrawing(group);
RenderTargetBitmap target = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
targetContext.Close();
target.Render(targetVisual);
BitmapFrame targetFrame = BitmapFrame.Create(target);
return targetFrame;
BitmapFrame newphoto = GetBitmapFrame(original, width, height, BitmapScalingMode.NearestNeighbor);

return GetBmpStream(newphoto);
}
}
}
75 changes: 75 additions & 0 deletions ImageResizer4DotNet/ImageResizer4DotNet/ResizerPrivate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ImageResizer4DotNet
{
public partial class Resizer
{
private static BitmapFrame GetBitmapFrame(MemoryStream original, int width, int height, BitmapScalingMode mode)
{
BitmapDecoder photoDecoder = BitmapDecoder.Create(original, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
BitmapFrame photo = photoDecoder.Frames[0];

TransformedBitmap target = new TransformedBitmap(
photo,
new ScaleTransform(
width / photo.Width * 96 / photo.DpiX,
height / photo.Height * 96 / photo.DpiY,
0, 0));
BitmapFrame thumbnail = BitmapFrame.Create(target);
BitmapFrame newPhoto = Resize(thumbnail, width, height, mode);

return newPhoto;
}

private static BitmapFrame Resize(BitmapFrame photo, int width, int height, BitmapScalingMode scalingMode)
{
DrawingGroup group = new DrawingGroup();
RenderOptions.SetBitmapScalingMode(group, scalingMode);
group.Children.Add(new ImageDrawing(photo, new Rect(0, 0, width, height)));
DrawingVisual targetVisual = new DrawingVisual();
DrawingContext targetContext = targetVisual.RenderOpen();
targetContext.DrawDrawing(group);
RenderTargetBitmap target = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
targetContext.Close();
target.Render(targetVisual);
BitmapFrame targetFrame = BitmapFrame.Create(target);
return targetFrame;
}

private static MemoryStream GetPngStream(BitmapFrame photo)
{
MemoryStream result = new MemoryStream();

PngBitmapEncoder targetEncoder = new PngBitmapEncoder();
targetEncoder.Frames.Add(photo);
targetEncoder.Save(result);

return result;
}

private static MemoryStream GetJpegStream(BitmapFrame photo)
{
MemoryStream result = new MemoryStream();

JpegBitmapEncoder targetEncoder = new JpegBitmapEncoder();
targetEncoder.Frames.Add(photo);
targetEncoder.Save(result);

return result;
}

private static MemoryStream GetBmpStream(BitmapFrame photo)
{
MemoryStream result = new MemoryStream();

BmpBitmapEncoder targetEncoder = new BmpBitmapEncoder();
targetEncoder.Frames.Add(photo);
targetEncoder.Save(result);

return result;
}
}
}

0 comments on commit 9683749

Please sign in to comment.