Using code

Shinnen

Member
Hi,
I have no idea where to post this, so here goes.
I want to reduce the size of a jpg, and at the same time maintain it's clarity.
None of the editors I have will do this. They all look blurry after the reduction.
So, I went on line looking for a program, and found a site that gives a code for doing this.
The problem is that I have NO idea what to do with this code OR how to use it on my
jpg. Here's the code. I'm hoping that someone can advise me about what to do with it.
Thanks,
...... john

publicvoid ResizeImage(double scaleFactor, Stream fromStream, Stream toStream)
{
var image = Image.FromStream(fromStream);
var newWidth = (int)(image.Width * scaleFactor);
var newHeight = (int)(image.Height * scaleFactor);
var thumbnailBitmap = newBitmap(newWidth, newHeight);

var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbnailGraph.DrawImage(image, imageRectangle);

thumbnailBitmap.Save(toStream, image.RawFormat);

thumbnailGraph.Dispose();
thumbnailBitmap.Dispose();
image.Dispose();
}
 
The issue is that you are reducing the resolution.

You are basically removing the detail. How do you represent a red, blue and yellow coloured trio of pixels in one pixel?

You will get this effect...
Caltex-Retina-Display-Pixel-Density-Graph-A.jpg

This is lossy. You cant reverse it...so if you blow it up to the same size as before, you have lost pixel density. It is blurred maybe az whatever you are using is trying to reducd the jagged edge effect? Like anti aliasing?

I am guessing this is C#.

Have you tried this code, what is happening, what is the output for your tests?

Are you restricted to C#? Java has some good graphics libraries iirc
 
Last edited:
Hi SpriteMidr,
You're dealing with a complete newbie. I have **NO** idea how to try the code.
....... john
 
Hi SpriteMidr,
You're dealing with a complete newbie. I have **NO** idea how to try the code.
....... john
Right...

First of all...how much do you know of how to use C#? You understand the basics of variables, functions, etc..? (No patronisation meant...just wanna make sure I am not baffling you with technical stuff if you are new :) )

<edit>

On reading the code in more detail, I think I can see what it is doing.

I have made a few changes just to make it make more sense, I am just gonna break the code down in snippets and try to explain what I am doing (blindly, as I have no VS installed ;) )

Namespaces to include:
you will need to import the system and System.Drawing namespaces
Code:
using System;
using System.Drawing;
using System.Drawing.Imaging; // for specifying output format of image
...this goes at the top of your file...

This code is from your original code, so I guess it works. Otherwise this wont work, but it should hopefully work. I can try it in VS once it has installed if you want me to.

Code:
// This will ask for the file to open (inputImageLocation)
// ... outputImageLocation to save the result to
// double scale factor
public void Resize(string inputImageLocation, string outputImageLocation, double scaleFactor)
{
  // Takes the file to open, use true to use embedded colour info
  Image inputImg = Image.FromFile(inputImageLocation, true);

  // Convert to a bitmap
  // JPEG is compressed, and bitmap is a type of Image (derived class)
  // We just use an explicit cast
  Bitmap inputBmp = (Bitmap) inputImg;

  // Get original dimensions
  uint originalWidth = inputImg.Width;
  uint originalHeight = inputImg.Height;

  // Convert to new dimensions
  uint newWidth = originalWidth * scaleFactor;
  uint newHeight = originalHeight * scaleFactor;

  // Make a new empty bitmap with the size we want
  Bitmap newBmp = new Bitmap(newWidth, newHeight);

  // Get the graphics object (holds gfx settings) from this new bitmap
  Graphics newGfx = Graphics.fromImage(newBmp);

  // Set quality settings to what you specified
  newGfx.CompositingQuality = CompositingQuality.HighQuality;
  newGfx.SmoothingMode = SmoothingMode.HighQuality;
  newGfx.InterpolationMode = InterpolationMode.HighQualityBicubic;

  // Make a new rectangle and draw image to it

  Rectangle newRgl = new Rectangle(0, 0, newWidth, newHeight);
  
  // Draw the image with the new dimensions of the rectangle
  // to the new bitmap
  newGfx.DrawImage(inputImg, newRgl);

  // We now have a raw bitmap object with (hopefully) the resized image.
  // So now, just save it to the new location
  // Use the image format of JPEG as you wanted
  // The data is in the graphics object (newGfx) that is inside this bitmap!
  newBmp.Save(outputImageLocation, ImageFormat.JPEG);
}

Don't take this as gospel, as it is untested, and the namespace declarations may be wrong. Until I can test this, I cant tell, as I am rusty with C#.
 
Last edited:
That looks like Java to me..

You aren't going to get different results than using something like 'resize' in paint or photo viewer.
 
That looks like Java to me..

You aren't going to get different results than using something like 'resize' in paint or photo viewer.

java doesn't have the var keyword. If it is from a decent source, I would infer it to be c# anyway, as java uses camelcase for function naming rather than pascalcase that C# favors. Var causes confusion anyway as it just masks the static typing behind it.
 
You aren't going to get different results than using something like 'resize' in paint or photo viewer.
You might get better results from downscaling in the gimp or photoshop than in paint.
I am guessing this is C#.
Yeah, looks like C# to me.

Seeing as you have no coding background I don't really expect you'd have VS to compile this. Your best bet is probably to grab gimp or photoshop and reduce the image dimensions without antialiasing the image (disable smoothing). You'll get some jaggy lines if you don't maintain aspect ratio but if you keep the dimensions relatively the same it might be what you want.


-------------------------------------------------
SpriteMidr has the main issue for what's happening. To put it as simple as I can, by making the image smaller you are losing coloured dots. So instead of 1000 dots, you might be going to 100 dots, and trying to keep the same colour information in a lot fewer spaces.



Are you trying to reduce the dimensions or the file size? If it's just file size, there might be a lot more options available.
 
Alright. I think maybe I'm in way over my head. I have no interest in learning code, or how to compile it. In my naivety I assumed that using this code was simply a matter of plugging it into a file, html or something(?), and running it. It's obviously is not that simple.
I'm not trying to reduce the file size. I simply want to paste the picture into the body of my email, without it taking up an enormous amount of real estate. In the past, I've simply used the image size reduction function in my viewer (FastStone), but the starting files were much larger (4000x3000 pixels) than the one I'm using this time (2047x1342), so the loss in image quality was barely noticeable (I'm assuming that's why it worked). I've heard that Gimp and Photoshop will reduce the image size without a noticeable loss in quality, so maybe that's what I have to do.
Thank you all for your help.
..... john
 
Alright. I think maybe I'm in way over my head. I have no interest in learning code, or how to compile it. In my naivety I assumed that using this code was simply a matter of plugging it into a file, html or something(?), and running it. It's obviously is not that simple.
I'm not trying to reduce the file size. I simply want to paste the picture into the body of my email, without it taking up an enormous amount of real estate. In the past, I've simply used the image size reduction function in my viewer (FastStone), but the starting files were much larger (4000x3000 pixels) than the one I'm using this time (2047x1342), so the loss in image quality was barely noticeable (I'm assuming that's why it worked). I've heard that Gimp and Photoshop will reduce the image size without a noticeable loss in quality, so maybe that's what I have to do.
Thank you all for your help.
..... john

C# is not too bad to learn. You just need a C# compiler to make it into a runable format.

HTML is just a way of describing a layout of a web page. CSS is making it look pretty, Javascript (+Jquery) is making it do more dynamic stuff.

All three are interpreted on the fly by your browser each time you open said page.

Java and C# have to be compiled (made) into a useful format, this makes them faster and you can do almost anything you want with them.

Go for GIMP unless you want to buy Photoshop. Paint.NET is meant to be decent too.
 
Hi,
I went onto the net, after sending my last mail, and found a site that examined several applications for reducing picture size, without loss of quality ..... here <https://sfaturiit.ro/en/resize-pictures-without-losing-quality/> The impression I got was that it's not possible to completely eliminate quality loss, but that some programs do better than others in that regard. Up to this point I had tried several of my own editors (Photofiltre, Fastone, Helicon filter) and a couple I downloaded which claimed to do same, but none of them really did. One of the programs the site mentioned is Irfanview, which I'm quite familar with, saying that with some tweaking it did a decent job. So .... I tried it, et voila, it did work fairly well.
Thank you all for your help.
.... john
 
There's definitely a difference in what you use. Even the scaling algorithm matters. In the attached, the top left is the original. Then I scaled the image down using cubic interpolation (left) and no interpolation (right) and repeated the process for scaling it up except I suck and reversed the sides for cubic vs none..... oops.

The down scaling is harder to notice a difference, but it you zoom stuff looks jagged
 

Attachments

  • example.png
    example.png
    178.9 KB · Views: 7
Back
Top