I want to reduce the the size of an image to fixed size using C# windowsforms.
For suppose am taking an image with size 3 MB , i want the output of the image size is in between 100 to 200 KB.
Note: The size of an input image may be vary but i need the output will be the same
There is no built-in functionality to resize an image to a given file size that I am aware of. The size can be reduced by various methods:
Changing to a compressed file format.
Shrinking the dimensions of the image.
Lowering the quality of the image.
The system would need a loop that attempts to resize the image using one more of the above methods, check what the resulting file size is, and then adjust the parameters and try again until the desired size is reached.
A more common way to deal with this is to shrink the dimensions of the image to a height and width that generally yields acceptable sizes and not worry about the exact size of any one file. There are numerous 3rd party libraries that will easily do this (I've used ImageResizer successfully), or try this.
Related
I have a bitmap image and I want to put it in excel. I used this code I found here:
xlWorkSheet.Shapes.AddPicture("C:\\filesystem\\mb2.bmp",
msoFalse, msoCTrue, 0, 0, 518, 390);
But the resulting image is 1.333 times wider and higher. OK, so I can just multiply the dimensions by 0.75 and I get an image in excel with the desired dimensions.
xlWorkSheet.Shapes.AddPicture("C:\\filesystem\\mb2.bmp",
msoFalse, msoCTrue, 0, 0, (float)(518*0.75), (float)(390*0.75));
But that number 0.75 sitting there hard-coded really bothers me. Especially since I've seen this question in which the op's ratio is 0.76. Knowing that this code needs to run on any number of systems with different displays, I want to know how to get the ratio programmatically.
Somewhat also related to this question which has to do with copy-paste without code.
If you are talking about printing, the display is irrelevant.
The dimensions of the image need to be relative to the paper size. The size values in the AddPicture method are in points and are only loosely related to pixels. Points are units if measure that make sense to your printer. The application translates points to pixels for you so you don't need to worry about that.
You can use the InchesToPoints or CentimetersToPoints methods of the Application object to size your image to paper size.
This is a follow on to my previous question Speeding Up Image Handling
Apologies if I should have amended that question in some way rather than starting a new one.
I have tried all sorts of different things to speed up the drawing of an image on screen.
I thought that compressing the image until it is smaller would have an effect. However while this might save memory for the object I do not think it has any effect on how long it takes to draw. I tried converting the image to jpeg and using 100% compression. However while this creates a blocky image it does not affect the drawing time. I now think this is because the number of pixels that get rendered is not changed by this
I tried reducing the color palette to 256 colors. This makes the size smaller since it uses less bytes per pixel but does not seem to affect drawing on screen. I had thought that reducing the bytes per pixel GDI+ has to handle might save some time but it is not enough for me to see so far.
So am I wasting my time looking at compression and palette?
I assume that time taken will be affected by the number of pixels to be drawn (width x height) and I have resized the image to match the pixel size as displayed on screen. I think this is the one thing that does have an effect....
I have looked at how to stop autoscaling of the image - does my resize stop that or can the image still be autoscaled when it is rendered?
I am wondering if I can replace the DrawImage call with something using p/Invoke or other API call (which I admit I don't really understand).
You could use the Bitblt P/Invoke, which copies image data directly. It does require some initialisation to convert the image data to something the display device understands, but this copy operation is lightning fast. If you would like to know what exactly is happening in DrawImage btw, use a tool like ILSpy or Reflector.NET to inspect the method.
See BitBlt code not working for an example. For some information about Bitblt see http://www.codeproject.com/KB/GDI-plus/flicker_free.aspx.
i have an algorithm that reduces the number of colors in a picture. I than save the picture with reduced number of colors(32 colors) as jpg. But the new jpg has the same size as the old.
Why is this and how can I reduce the size?
The JPEG file format doesn't use a palette, so it still stores all 24 bits of color information for each pixel, even if there are only a few different colors.
You reduce the size of a JPEG image by changing the compression ratio. Higher compression means smaller file size, but also more compression artifacts.
If you want to reduce the file size by reducing the number of colors, you would have to save it using a file format that uses a palette, like GIF or PNG-8.
A JPEG is always saved with 24-bit color. The JPEG algorithm is designed for saving full-color photographs (that's what the "P" stands for), not reduced-color bitmaps. Reducing the number of colors introduces sharp transitions between colors that use up extra space within the JPEG file.
The proper way to reduce the size of a JPEG is to decrease the quality of the file.
However, it's probably better for you to use a file format that's intended for saving reduced-color images, like PNG or GIF.
When people write an article, they submit a photo to illustrate the event. But, the space to display is not wide. So, I'd like to reduce they width and/or height while keeping their original proportion. Otherwise, the image gets distorted.
Let's say the max width is 300px. Anyting wider than that would see its width reduced to 300px. However, I'd like the Height to be reduced to the same proportion. For instance: 600 x 800 will become 300px x 400px. So, I need to be able to check the dimensions
How do I check those dimensions?
When do I check them? (i) after uploading, (ii) while retreiving from the database?...
Thanks for helping
I would suggest you check the image after upload and save two versions to the database - the original and the resized version. You should serve the resized versions with the article. That way you resize the image only once.
For code example take a look here:
C#: Resize An Image While Maintaining Aspect Ratio and Maximum Height
I m using C#.NET 4.0
I have an jpeg image and i want to reduce its size in bytes .I don't want to change the image size in manner of height and width and not want to lose image quality.Some bit of reduce quality is not an issue. I try to make it a thumbnail image but it reduce the size according to height and width.
I can't found any solution.
Any type help will be appreciated..
Size reduction is generally accompanied by quality reduction. You cannot have it both ways.
How about saving jpeg at 90% quality (which usually provides good quality of an image) and then comparing size with your original jpeg file.
Something like this:
new jpeg < old jpeg ? new jpeg : old jpeg
If you want reduce the image size then you have to trim the width and height, according to that only the size reduces unless it is not possible.