0

i try to explain what i need.

1 -My goal condition is to transform black pixels of image in Cartesian points without resize image. (ok, Done)

2 -Second goal condition is to resize image and redo step 1. (ok, i have normalized points with original image size and the job is done when changed width or height!)

3 - Now i need to reduced number of pixels in image to have defined number of DPI in my image to redo step 1. How? I have found the method setResolution(..,..) but how i must change the width and height of my image to obtain the correct resolution in terms of DPI? (see [?????] in code )

         var image2 = new Bitmap(canvasWidth, canvasHeight);
     image2.SetResolution(200.0f,200.0f); // I need this for example!
     using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(image2)) {
        gr.SmoothingMode = SmoothingMode.HighSpeed;
        gr.InterpolationMode = InterpolationMode.Low;
        gr.PixelOffsetMode = PixelOffsetMode.None;
        gr.Clear(Color.White);
        gr.DrawImage(this.LoadedImage, new System.Drawing.RectangleF(new PointF((float)this.Centre.x, (float)this.Centre.y), new Size(canvasWidth [?????], canvasHeight[??????])));
        return image2;
     }

If i run over everyone pixel in my new image2 i have the same result like run over OriginalImage. Well,in the end i need to reduced the number of pixels of my image to obtain a defined Dots Per Inch result.

I hope I was clear.

Thanks.

  • Is that can help you? [How to resize an image in C#](https://stackoverflow.com/questions/1922040/how-to-resize-an-image-c-sharp) –  Nov 05 '19 at 10:52
  • You can use the overload of `DrawImage()` that lets specify the destination Rectangle and the Source rectangle. Set `InterpolationMode.HighQualityBicubic` and `PixelOffsetMode.Half`. GDI+ will do the work for you. If you're using *pixellated* images. If not, it would be helpful to see a sample of the Images you're working with and the code in `step 1`. – Jimi Nov 05 '19 at 10:59
  • `.Clear(Color.White);` is not necessary, since you're filling in the entire Bitmap. Unless you have (semi-)transparent pixels you want to turn to white(-ish). – Jimi Nov 05 '19 at 11:07
  • SetResolution() is widely misunderstood. It is just a reference number that describes the device that was used to create the image. Intention is to get the image displayed with the same *physical* size on another machine. It will rescale as necessary, like Graphics.DrawImage() does. Intention is to prevent it from turning into a postage stamp on an expensive monitor or printer. There is no device that records at 200 dpi, so it doesn't make much sense to do this. – Hans Passant Nov 05 '19 at 11:09
  • SetResolution(200.0f,200.0f) is only for example. In a few words, I need to transform the image loaded in geometric object. My algorithm runs all pixels in image and if the pixel's color is black i must create a Point(). This system work fine, but when i resize image to small one, if i use the "Normalize points" i have a bad result because the points are almost overlapping (and this is correct!). For this reason i should change the density of pixels in resized image. (I work with black and white images). – The Overrider Nov 05 '19 at 12:26
  • @Jimi yes is correct! But how can i find the correct width and height of rectangle to have correct dpi? – The Overrider Nov 05 '19 at 12:31
  • Setting the resolution (bad name, btw), doesn't change the *density*. You cannot add what's not there. You can interpolate the pixels that are present in the source image, but you cannot *generate* new ones. So, no *density* increase. – Jimi Nov 05 '19 at 12:35
  • @Jimi, infact i want to reduce, i start with big image (ex 1536x2048). – The Overrider Nov 05 '19 at 12:39
  • @Jimi i must find the correct value of { canvasWidth [?????] & canvasHeight[?????]} (less than original) to have my DPI value. – The Overrider Nov 05 '19 at 12:47
  • @HansPassant infact i need something like this...i must find the correct value of { canvasWidth [?????] & canvasHeight[?????]} (less than original) to have my DPI value. – The Overrider Nov 05 '19 at 12:52
  • That doesn't make any sense. Instead of talking about the solution, be sure to describe the problem you are trying to solve. – Hans Passant Nov 05 '19 at 12:56

0 Answers0