Crop a Graphics object? - asp.net

I need to do two things to an image: resize it, then crop it.
I'm resizing like this:
nonResizedImage = new Bitmap(imagePath);
Bitmap scaled = new Bitmap(preCropWidth, preCropHeight);
using (Graphics scaledGraphics = Graphics.FromImage(scaled))
{ // scale image to the sizeo f the image the user cropped on
scaledGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
scaledGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
scaledGraphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
scaledGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
scaledGraphics.Clear(ColorTranslator.FromHtml("#FFFFFF"));
scaledGraphics.DrawImage(nonResizedImage, 0, 0, preCropWidth, preCropHeight);
}
Now I need to crop the image. I've found a function that does this:
static byte[] Crop(string Img, int Width, int Height, int X, int Y)
{
try
{
using (SD.Image OriginalImage = SD.Image.FromFile(Img))
{
using (SD.Bitmap bmp = new SD.Bitmap(Width, Height))
{
bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
{
Graphic.SmoothingMode = SmoothingMode.AntiAlias;
Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
Graphic.DrawImage(OriginalImage, new SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, OriginalImage.RawFormat);
return ms.GetBuffer();
}
}
}
}
catch (Exception Ex)
{
throw Ex;
}
}
But this requires a image as input. So, I could save the output of my resize code to the disk, then read it back in again to do the crop, but this seems needlessly inefficient. I don't really know much about image manipulation in c# though.
How do I crop the scaledGraphics I have, without first saving it to the disk?

One of the overloads for new Bitmap is Width, Height, Graphics Object. You should be able to just pass the graphics object in and then create the bitmap from that. Something like this
static byte[] Crop(Graphics g, int Width, int Height, int X, int Y)
{
try
{
using (Bitmap bmp = new Bitmap(Width, Height, g))
{
...
}
}
......
}

Related

JavaFX: Convert Image to Greysacale

I need to do that in my program. I have to do it in two ways:
1.) by my own, with the following code:`
private Image convertToGrayScale(Image image) {
WritableImage result = new WritableImage((int) image.getWidth(), (int)
image.getHeight());
PixelReader preader = image.getPixelReader();
PixelWriter pwriter = result.getPixelWriter();
for (int i = 0; i < result.getWidth(); i++) {
for (int j = 0; j < result.getHeight(); j++) {
Color c = preader.getColor(i, j);
double red = (c.getRed() * 0.299);
double green = (c.getGreen() * 0.587);
double blue = (c.getBlue() * 0.114);
double sum = c.getRed() + c.getBlue() + c.getGreen();
pwriter.setColor(i , j, new Color(sum, sum, sum, 1.0));
}
}
return result;
}
2.) with the help of the openCV library, with the following code (it was copied almost perfectly from their site) :
public WritableImage loadAndConvert() throws Exception {
//Loading the OpenCV core library
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
String input = "C:/Users/Dan Ivgy/eclipse-workspace/LuckyBride/sample/20180402_170204.jpg";
//Reading the image
Mat src = Imgcodecs.imread(input);
//Creating the empty destination matrix
Mat dst = new Mat();
//Converting the image to gray scale and saving it in the dst matrix
Imgproc.cvtColor(src, dst, Imgproc.COLOR_RGB2GRAY);
// Imgcodecs.imwrite("C:/opencv/GeeksforGeeks.jpg", dst);
//Extracting data from the transformed image (dst)
byte[] data1 = new byte[dst.rows() * dst.cols() * (int)(dst.elemSize())];
dst.get(0, 0, data1);
//Creating Buffered image using the data
BufferedImage bufImage = new BufferedImage(dst.cols(),dst.rows(),
BufferedImage.TYPE_BYTE_GRAY);
//Setting the data elements to the image
bufImage.getRaster().setDataElements(0, 0, dst.cols(), dst.rows(), data1);
//Creating a WritableImage
WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
System.out.println("Converted to Grayscale");
return writableImage;
}
In both cases, the problem was that i have'nt got a "greyscale" output, just somthing different (and before you
asked: yeas, i have tried to to do it on several pictures, not only one)
Here's the input picture and the output picture:
Well, as you can see, this is Not a greyscale! maybe a sunset-scale..
I'd really appraciate any help, Thank you :)
(espcially if there's something faster out there since those solutions rather takes a while to run)
If someone knows, why there is not some built in option in javaFX as there is a lot of sophisticated imageview effects and this one is so simple and so prevalent.
UPDATE:
i found a website that do somthing similar to ehat i did - and somehow i got a different output! i don't get it
here's the website.
and here's the output from my computer:
my output
UPDATE#2: as #matt correctly asked, here's the code that use this method:
ImageIO.write(SwingFXUtils
.fromFXImage
(convertToGrayScale(new Image(getClass().getResource("1_CNc4RxV85YgthtvZh2xO5Q.jpeg").toExternalForm()) ), null), "jpg", file);
the original target was to show the image to rhe user, and the problem was there, so i changed the code to this one which save the image so i could isolate the problem more easly..
Ok guys, after some research, and endless number of attememts i got what i need
to solve the problem ill show my solution here and come to a coclude of my insights about this issue..
first, ill give the the disticntion between "SAVING the file" and "Setting the file in the ImageView.
when we want just to show it in image view, i have'ne expereinced any problem using almost every solution suggested here. the most simple, short and sweet (in my opinion) is the following one:
private Image convertToGrayScale(Image image) {
WritableImage result = new WritableImage((int) image.getWidth(), (int) image.getHeight());
PixelReader preader = image.getPixelReader();
PixelWriter pwriter = result.getPixelWriter();
for (int i = 0; i < result.getWidth(); i++)
for (int j = 0; j < result.getHeight(); j++)
pwriter.setColor(i , j, preader.getColor(i, j).grayscale());
return result;
}
(for convinience, i ignored exception handling)
it works fine when i use it along when i use it along with the following code:
Image img1 = convertToGrayScale(new Image(filepath);
imageView.setImage(img1);
about SAVING this output image, after some research and using #trashgold 's references, and this important one :
i got my solution as the following:
private void saveBadImage(BufferedImage originalImage, File dest) throws IOException
{
// use the following line if you want the first parameter to be a filepath to src image instead of Image itself
//BufferedImage originalImage = ImageIO.read(file);
// jpg needs BufferedImage.TYPE_INT_RGB
// png needs BufferedImage.TYPE_INT_ARGB
// create a blank, RGB, same width and height
BufferedImage newBufferedImage = new BufferedImage(
originalImage.getWidth(),
originalImage.getHeight(),
BufferedImage.TYPE_INT_RGB);
// draw a white background and puts the originalImage on it.
newBufferedImage.createGraphics().drawImage(originalImage,0,0,null);
// save an image
ImageIO.write(newBufferedImage, "jpg", dest);
}
and, i use it with the following code:
Image img1 = convertToGrayScale(new Image(filepath));
BufferedImage img2 = new BufferedImage((int) img1.getWidth(), (int) img1.getHeight(), BufferedImage.TYPE_INT_ARGB);
saveBadImage(img2, file);
and, it works perfectly!
Thank you all guys, and i hope my insights will help to some people
I decided to work in awt, then create a javafx image.
public class App extends Application {
#Override
public void start(Stage stage) {
WritableImage gray = null;
try {
BufferedImage awtImage = ImageIO.read(new URL("https://i.stack.imgur.com/ysIrl.jpg"));
gray = new WritableImage(awtImage.getWidth(), awtImage.getHeight());
BufferedImage img2 = new BufferedImage(awtImage.getWidth(), awtImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
for(int i = 0; i<awtImage.getWidth(); i++){
for(int j = 0; j<awtImage.getHeight(); j++){
int c = awtImage.getRGB(i, j);
int r = (c>>16)&255;
int g = (c>>8)&255;
int b = (c)&255;
int s = (int)(r*0.299 + g*0.587 + b*0.114);
int gr = (255<<24) + (s<<16) + (s<<8) + s;
img2.setRGB(i, j, gr);
}
}
gray = SwingFXUtils.toFXImage(img2, gray);
} catch (IOException e) {
e.printStackTrace();
}
ImageView view = new ImageView(gray);
ScrollPane pane = new ScrollPane(view);
Scene scene = new Scene(new StackPane(pane), 640, 480);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Both methods work for me, so I'm not sure if this helps you. I can save the result too.
System.out.println("saving " +
ImageIO.write(img2, "PNG", new File("tested.png")) );
If you're using oracles jdk, I think you can save JPEG. I am using OpenJDK which doesn't seem to be able to write JPEG.

How to generate image Thumb minimal coding

How to generate image as Thumb in simple way i think minimal coding on asp.net,
Then after how to save particular folder
I would not recommend to use the "Image.GetThumbnailImage" method because of the poor quality
(search "Image.GetThumbnailImage quality" on your favorite search engine...)
Here is a code snippet:
void GenerateThumbnail(string sourceImagePath, string destImagePath, int width, int height, System.Drawing.Imaging.ImageFormat destImageFormat)
{
using (var destImage = new System.Drawing.Bitmap(width, height))
{
using (var g = System.Drawing.Graphics.FromImage(destImage))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
using (var sourceImage = System.Drawing.Image.FromFile(sourceImagePath))
{
g.DrawImage(sourceImage, new System.Drawing.Rectangle(0, 0, width, height));
}
destImage.Save(destImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
OR - you can use Piczard (Piczard)
Here are some usage examples:
// 50% thumbnail (Jpeg image format)
var filter = new CodeCarvings.Piczard.ImageTransformation(50);
filter.SaveProcessedImageToFileSystem("~/MySourceImage.jpg", "~/MyDestImage.jpg");
// Fixed size thumbnail 400x200 (PNG image format)
var filter2 = new CodeCarvings.Piczard.FixedResizeConstraint(400, 200);
filter.SaveProcessedImageToFileSystem("~/MySourceImage2.jpg", "~/MyDestImage2.png");
I have tried like this so cool..!!,
public void ThumbGenerate(string sourcepath,string thumbsavepath, int width, int height)
{
Image image = Image.FromFile(sourcepath);
Image thumb = image.GetThumbnailImage(width, height, () => false, IntPtr.Zero);
thumb.Save(Path.ChangeExtension(thumbsavepath, "jpg"));
}
Note :You can change extension what you like something jpg,jpeg,png

Replace GDI+ DrawImage with PInvoked GDI and transparent PNG's

I've created an image service in C# which takes a base layer image (JPG), layers one more more transparent PNG's (32 bit), and then outputs a final JPG image. I'm trying to squeeze every last millisecond out of this function and my code is bottlenecking at the DrawImage call in GDI+. Managed code here:
// Load base image and create graphics
Image image = LoadImage(renderSettings.RenderedImageDirectory + baseLayer);
Graphics graphics = Graphics.FromImage(image);
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
// Draw additional layers to final image
for (int i = 1; i < renderLayers.Count; i++) {
// SLOW -- LoadImage just a utility method that returns an Image from disk or cache
graphics.DrawImage(LoadImage(renderSettings.RenderedImageDirectory + renderLayers[i]), 0, 0, image.Width, image.Height);
}
if (graphics != null) graphics.Dispose();
Now, I read about the performance gains obtained by calling GDI directly by P/Invoke and made an attempt at replacing the DrawImage call. I created a unit test to try to duplicate the same functionality of loading a JPG and then layering one transparent PNG on top of it.
Ref: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/29582142-0068-40dd-bd99-4b3883a76350
Bitmap sourceImage = new Bitmap("c:\\base.jpg");
Bitmap overlayImage = new Bitmap("c:\\layer1.png");
// NOTE: ImageHelper is a utility class containing all the P/Invoke stuff
// Get source image in memory
Graphics sourceImageGraphics = Graphics.FromImage(sourceImage);
IntPtr sourceImageHDC = sourceImageGraphics.GetHdc();
IntPtr sourceImageCDC = ImageHelper.CreateCompatibleDC(sourceImageHDC);
IntPtr sourceImageHandle = sourceImage.GetHbitmap();
ImageHelper.SelectObject(sourceImageCDC, sourceImageHandle);
// Get overlay image in memory
Graphics overlayImageGraphics = Graphics.FromImage(overlayImage);
IntPtr overlayImageHDC = overlayImageGraphics.GetHdc();
IntPtr overlayImageCDC = ImageHelper.CreateCompatibleDC(overlayImageHDC);
IntPtr overlayImageHandle = overlayImage.GetHbitmap();
ImageHelper.SelectObject(overlayImageCDC, overlayImageHandle);
ImageHelper.BitBlt(sourceImageHDC, 0, 0, sourceImage.Width, sourceImage.Height, overlayImageCDC, 0, 0, ImageHelper.TernaryRasterOperations.SRCAND);
ImageHelper.AlphaBlend(sourceImageHDC, 0, 0, sourceImage.Width, sourceImage.Height, overlayImageCDC, 0, 0, sourceImage.Width, sourceImage.Height, new ImageHelper.BLENDFUNCTION(ImageHelper.AC_SRC_OVER, 0, 0xff, ImageHelper.AC_SRC_ALPHA));
// Release source Image memory.
ImageHelper.DeleteDC(sourceImageCDC);
ImageHelper.DeleteObject(sourceImageHandle);
sourceImageGraphics.ReleaseHdc(sourceImageHDC);
sourceImageGraphics.Dispose();
// Release overlay Image memory.
ImageHelper.DeleteDC(overlayImageCDC);
ImageHelper.DeleteObject(overlayImageHandle);
overlayImageGraphics.ReleaseHdc(overlayImageHDC);
overlayImageGraphics.Dispose();
// Save to jpg
sourceImage.Save("c:\\output.jpg", ImageFormat.Jpeg);
But this fails to produce a layered image. Just the PNG without the base JPG. What should I be doing differently? I'm a little out of my league when in comes to straight GDI.
I ended up using SharpDX to access both the WIC and Direct2d API's. The results are impressive to say the least. When compositing with Direct2d I'm seeing increased performance as much as 400-500% over GDI+.
I also tried GDI+ and the Task Parallel Library to break up images into four quandrants and do compositing work in each core. The results weren't nearly as signficant as using SharpDX.
Here's the code I ended up using. The reference to "renderSettings" is just a configuration object. Substitute as needed along with the renderLayer image list.
/* SharpDX */
using SharpDX;
using SharpDX.Direct2D1;
using SharpDX.DirectWrite;
using SharpDX.DXGI;
using SharpDX.IO;
using SharpDX.WIC;
using AlphaMode = SharpDX.Direct2D1.AlphaMode;
using WicBitmap = SharpDX.WIC.Bitmap;
using D2DPixelFormat = SharpDX.Direct2D1.PixelFormat;
using WicPixelFormat = SharpDX.WIC.PixelFormat;
using Rectangle = System.Drawing.Rectangle;
using Bitmap = System.Drawing.Bitmap;
public Image FlattenImageDirect2d()
{
List<string> renderLayers = new List<string>()
{
"image1.jpg", "image1.png", "image2.png", "image3.png", "image4.png", "image5.png", "image6.png", "image7.png"
};
// Base image
string baseLayer = renderLayers[0];
// Create WIC and D2D factories
var wicFactory = new ImagingFactory();
var ddFactory = new SharpDX.Direct2D1.Factory();
// Get image size using WIC
int baseWidth, baseHeight;
using (var wicStream = new WICStream(wicFactory, renderDirectory + baseLayer, NativeFileAccess.Read)) {
var jpegDecoder = new JpegBitmapDecoder(wicFactory);
jpegDecoder.Initialize(wicStream, DecodeOptions.CacheOnDemand);
var frame = jpegDecoder.GetFrame(0);
baseWidth = frame.Size.Width;
baseHeight = frame.Size.Height;
frame.Dispose();
jpegDecoder.Dispose();
}
// Resize image?
bool resizeImage = (baseWidth != renderSettings.RenderWidth) || (baseHeight != renderSettings.RenderHeight);
// Bitmaps and render target settings
var wicBitmap = new WicBitmap(wicFactory, renderSettings.RenderWidth, renderSettings.RenderHeight, SharpDX.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnLoad);
var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new D2DPixelFormat(Format.Unknown, AlphaMode.Unknown), 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT);
var wicRenderTarget = new WicRenderTarget(ddFactory, wicBitmap, renderTargetProperties);
// Create bitmap render target used to draw all images to
SharpDX.Direct2D1.BitmapRenderTarget bitmapRenderTarget = new SharpDX.Direct2D1.BitmapRenderTarget(wicRenderTarget, CompatibleRenderTargetOptions.None, new D2DPixelFormat(Format.Unknown, AlphaMode.Premultiplied));
// Draw render layers
for (int i = 0; i < renderLayers.Count; i++) {
// First layer is always a jpeg, all other subsequent layers are png's
ImageFormat imageFormat = (i == 0) ? ImageFormat.Jpeg : ImageFormat.Png;
using (SharpDX.WIC.BitmapSource bitmapSource = LoadWicBitmap(wicFactory, renderDirectory + renderLayers[i], imageFormat, resizeImage, renderSettings.RenderWidth, renderSettings.RenderHeight)) {
// Convert WIC pixel format to D2D1 format
var formatConverter = new FormatConverter(wicFactory);
formatConverter.Initialize(bitmapSource, SharpDX.WIC.PixelFormat.Format32bppPBGRA, BitmapDitherType.None, null, 0f, BitmapPaletteType.MedianCut);
// Create direct 2d bitmap from wic bitmap
SharpDX.Direct2D1.Bitmap direct2DBitmap = SharpDX.Direct2D1.Bitmap.FromWicBitmap(bitmapRenderTarget, formatConverter);
// Draw direct2d image to bitmap render target
wicRenderTarget.BeginDraw();
wicRenderTarget.DrawBitmap(direct2DBitmap, 1.0f, SharpDX.Direct2D1.BitmapInterpolationMode.Linear);
wicRenderTarget.EndDraw();
// Clean up
formatConverter.Dispose();
direct2DBitmap.Dispose();
}
}
// Final image data
byte[] imageData;
// Create streams to write output to.
using (var memoryStream = new MemoryStream()) {
using (var wicStream = new WICStream(wicFactory, memoryStream)) {
// Encode wic bitmap
var encoder = new JpegBitmapEncoder(wicFactory);
encoder.Initialize(wicStream);
var frameEncoder = new BitmapFrameEncode(encoder);
frameEncoder.Initialize();
frameEncoder.SetSize(renderSettings.RenderWidth, renderSettings.RenderHeight);
frameEncoder.PixelFormat = WicPixelFormat.FormatDontCare;
frameEncoder.WriteSource(wicBitmap);
frameEncoder.Commit();
encoder.Commit();
// Set image data
memoryStream.Position = 0;
imageData = memoryStream.ToArray();
// Clean up
frameEncoder.Dispose();
encoder.Dispose();
wicBitmap.Dispose();
wicRenderTarget.Dispose();
bitmapRenderTarget.Dispose();
ddFactory.Dispose();
wicFactory.Dispose();
frameEncoder = null;
encoder = null;
wicBitmap = null;
wicRenderTarget = null;
bitmapRenderTarget = null;
ddFactory = null;
wicFactory = null;
}
}
return Image.FromStream(new MemoryStream(imageData));
}
private BitmapSource LoadWicBitmap(ImagingFactory wicFactory, string path, ImageFormat imageFormat, bool resize, int resizeWidth = 0, int resizeHeight = 0)
{
PngBitmapDecoder pngDecoder;
JpegBitmapDecoder jpegDecoder;
BitmapFrameDecode bitmapFrameDecode;
var stream = new WICStream(wicFactory, path, NativeFileAccess.Read);
// Load the appropriate decoder
if (imageFormat == ImageFormat.Jpeg) {
jpegDecoder = new JpegBitmapDecoder(wicFactory);
jpegDecoder.Initialize(stream, DecodeOptions.CacheOnLoad);
bitmapFrameDecode = jpegDecoder.GetFrame(0);
jpegDecoder.Dispose();
}
else {
pngDecoder = new PngBitmapDecoder(wicFactory);
pngDecoder.Initialize(stream, DecodeOptions.CacheOnDemand);
bitmapFrameDecode = pngDecoder.GetFrame(0);
pngDecoder.Dispose();
}
// Clean up
stream.Dispose();
// Resize if necessary
if (resize) {
// Prepare scaler
var scaler = new BitmapScaler(wicFactory);
scaler.Initialize(bitmapFrameDecode, resizeWidth, resizeHeight, SharpDX.WIC.BitmapInterpolationMode.Fant);
return (BitmapSource)scaler;
}
return (BitmapSource)bitmapFrameDecode;
}
This one should work:
private Bitmap GetImage() {
//##################### Get the Bitmaps ############################
Bitmap sourceImage = new Bitmap("c:\\1.png");
Bitmap overlayImage = new Bitmap("c:\\2.png");
//##################### Get Hdc from baselayer ############################
Graphics sourceImageGraphics = Graphics.FromImage(sourceImage);
IntPtr sourceImageHDC = sourceImageGraphics.GetHdc();
//##################### Get Cdc from second layer ############################
IntPtr overlayImageCDC = CreateCompatibleDC(sourceImageHDC);
IntPtr overlayImageHandle = overlayImage.GetHbitmap();
SelectObject(overlayImageCDC, overlayImageHandle);
/*
* BitBlt from sourceImage is not neccessary,
* because Graphics.FromImage(sourceImage) already did it for you
*/
//##################### Draw the second layer ############################
AlphaBlend(sourceImageHDC, 0, 0, overlayImage.Width, overlayImage.Height, overlayImageCDC, 0, 0, overlayImage.Width, overlayImage.Height, new BLENDFUNCTION(AC_SRC_OVER, 0, 0xff, AC_SRC_ALPHA));
//##################### Release everthing ############################
sourceImageGraphics.ReleaseHdc(sourceImageHDC);
sourceImageGraphics.Dispose();
DeleteDC(overlayImageCDC);
DeleteObject(overlayImageHandle);
//##################### Return Image ############################
return sourceImage;
}

Actionscript/Air/Flex 4 - Load Image from File, Scale Down, and Save as PNG

I wrote these functions which I use in conjunction to scale down some BitmapData and save it as a PNG. However, the scaledImage.draw(originalImage, scalingMatrix, null, null, null, true); line with smoothing set to true does not have the intended smoothing effect when I save the bitmap data as a PNG using the second function. The resulting image file is not antialiased at all. Is there anything I'm doing wrong here? Thanks!
public static function scaleImage(originalImage:BitmapData, size:int):BitmapData
{
// Calculate the scaled size.
var scale:Number;
var scaledWidth:Number;
var scaledHeight:Number;
if (originalImage.width > originalImage.height)
{
scale = (size as Number) / (originalImage.width as Number);
scaledWidth = size;
scaledHeight = originalImage.height * scale;
}
else
{
scale = (size as Number) / (originalImage.height as Number);
scaledHeight = size;
scaledWidth = originalImage.width * scale;
}
var scalingMatrix:Matrix = new Matrix();
scalingMatrix.scale(scale, scale);
// Scale the image.
var scaledImage:BitmapData = new BitmapData(scaledWidth, scaledHeight, true, 0x00000000);
scaledImage.draw(originalImage, scalingMatrix, null, null, null, true);
return scaledImage;
}
public static function saveImageAsPNG(image:BitmapData, imageFile:File):void
{
// Encode the image as a PNG.
var pngEncoder:PNGEncoder = new PNGEncoder();
var imageByteArray:ByteArray = pngEncoder.encode(image);
// Write the image data to a file.
var imageFileStream:FileStream = new FileStream();
imageFileStream.open(imageFile, FileMode.WRITE);
imageFileStream.writeBytes(imageByteArray);
imageFileStream.close();
}
Turns out this code was working. It was saving smoothed images. It wasn't apparent because the images I was scaling down were ~20K x 20K pixels so jaggies appeared anyway with smoothing. Smoothing was apparent with more "normal" sized images like 2K x 2K pixels

Adapt asp.net code to resize image

After searching, I've discovered this code:
Public Sub ResizeImage(ByVal scaleFactor As Double, ByVal fromStream As Stream, ByVal toStream As Stream)
Dim image__1 = System.Drawing.Image.FromStream(fromStream)
Dim newWidth = CInt(image__1.Width * scaleFactor)
Dim newHeight = CInt(image__1.Height * scaleFactor)
Dim thumbnailBitmap = New System.Drawing.Bitmap(newWidth, newHeight)
Dim thumbnailGraph = System.Drawing.Graphics.FromImage(thumbnailBitmap)
thumbnailGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
thumbnailGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
thumbnailGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
Dim imageRectangle = New System.Drawing.Rectangle(0, 0, newWidth, newHeight)
thumbnailGraph.DrawImage(image__1, imageRectangle)
thumbnailBitmap.Save(toStream, image__1.RawFormat)
thumbnailGraph.Dispose()
thumbnailBitmap.Dispose()
image__1.Dispose()
End Sub
There are 2 things I can't "modify" to solve my problem:
I wouldn't like to pass a stream, but I prefer to pass a path like C:\mysite\photo\myphoto.gif. How can I "convert" it to accept a file and not a stream?
In this function I've to pass a "scale" value. But I prefer to check if the image is too big (for example > 1024x768) than resize it to a max of 1024x768. How can I check this with System.Drawing.
As you can see I don't know anything about System.Drawing so I need an "hard" help to solve this job.
Here is some c# code I did about 5 years ago to do this (it should still work I hope as the app hasn't been touched since). I think it does everthing you need but it doesn't upscale the image to 1024x768 if it is smaller. This code will only make sure that if it is larger than 1024x768, it will resize proportionally to fit within those dimensions:
const int maxWidth = 1024;
const int maxHeight = 768;
Image newImage = Image.FromFile("YourPicture.jpg");
double percentToShrink = -1;
if (newImage.Width >= newImage.Height)
{
// Do we need to resize based on width?
if (newImage.Width > maxWidth)
{
percentToShrink = (double)maxWidth / (double)newImage.Width;
}
}
else
{
// Do we need to resize based on width?
if (newImage.Height > maxHeight )
{
percentToShrink = (double)maxHeight / (double)newImage.Height;
}
}
int newWidth = newImage.Width;
int newHeight = newImage.Height;
// So do we need to resize?
if (percentToShrink != -1)
{
newWidth = (int)(newImage.Width * percentToShrink);
newHeight = (int)(newImage.Height * percentToShrink);
}
// convert the image to a png and get a byte[]
MemoryStream imgStream = new MemoryStream();
Bitmap bmp = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(bmp))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(System.Drawing.Brushes.White, 0, 0, newWidth, newHeight);
g.DrawImage(newImage, 0, 0, newWidth, newHeight);
}
// This can be whatever format you need
bmp.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png);
byte[] imgBinaryData = imgStream.ToArray();
imgStream.Dispose();
If you need to convert this to VB.NET, you can use the C# to VB.NET converter here.
First question:
Dim newImage As Image = Image.FromFile("SampImag.jpg")
Second question:
Build a private method that will return you a Size object based on the original Size object of the given image. You can add a "keep proportions" flag also if you wish.

Resources