0

How can I open an image file as bitmap scaled down?

For example in java I can do this:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap myBitmap = BitmapFactory.decodeFile(fname, options);

(this will load the image file as bitmap but scaled down by 2, means it loads only half of the image file's pixels)

How do I do the same thing in c#?

Flafy
  • 176
  • 1
  • 3
  • 15
  • 1
    Possible duplicate of [How to resize an Image C#](https://stackoverflow.com/questions/1922040/how-to-resize-an-image-c-sharp) –  Nov 01 '19 at 18:53
  • 1
    What are you targetting: Winforms, WPF, ASP..? YOU should __always__ TAG your questions correctly so one can see it on the questions page! – TaW Nov 01 '19 at 19:34
  • @TaW This is a class-library(.net framework) project and I'm exporting this as a DLL. I don't really know how to answer to your question because I don't know what's that – Flafy Nov 01 '19 at 19:44
  • 1
    There are several .Net frameworks you can base your code on, most common Winforms&GDI+ and WPF, but there are newer incarnations as well. A common choice is to use the one, the consumers of the DLL wil be using.. – TaW Nov 01 '19 at 20:08

1 Answers1

0

Using How to resize an Image C# and an extension method that replace decodeFile:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

static public class ImageHelper
{

  static public Bitmap LoadBitmap(this string filename, double scale = 1)
  {
    var image = Image.FromFile(filename);
    return ResizeImage(image, (int)(image.Width * scale), (int)(image.Height * scale));
  }

  // ResizeImage code here

}

Usage:

string filename = "path";

var bitmap1 = filename.LoadBitmap(0.5);

var bitmap2 = "path".LoadBitmap(0.5);

var bitmap3 = ImageHelper.LoadBitmap("path", 0.5);