-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added separate methods for PNG, JPEG, and BMP resizing. Moved private…
… methods to a partial class. Updated to version 2.5.
- Loading branch information
Showing
5 changed files
with
143 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |