Decrease Memory Usage Loading Bitmap (Win2D) - c#

I'm writing a program that uses Win2D to load an image file and display it on the screen. However, the image file itself is about 5 Mb in size, but when I load it using CanvasBitmap.LoadAsync, the process memory jumps up to over 600MB in memory and then settles down to around 300MB. Is there any way to reduce the process memory without having to resize the image manually in an image editor? I've seen code for resizing other types of bitmaps and I was wondering if that's also possible in Win2D.
Regards,
Alex
Update (1/27/2020)
Realized that bitmaps are uncompressed image files, so the only avalible options are either to reduce the image size somehow, or use a different file format. Decided to use the later because I'm working with PDF files. They can be converted into SVG files using Inkscape. Also, SVG files conveniently happen to be supported by Win2D.

Related

Magick.NET C# - huge memory usage

I'm struggling with Magick.NET library, as when converting image files to pdf's my memory usage is over 4GB and the CPU usage is 100%. When the conversion is done it all backs to normal. but as I'm using this particular third party in many instances of one application it causes huge memory loss.
Problem exists on line images.Write(newPdfPath);
using (MagickImageCollection images = new MagickImageCollection())
{
images.Read(orginalImage);
images.Write(newPdfPath);
}
The images are different sizes, and it really doesn't matter how big, as when converting jpg of size 7 KB the issue also exists.
Please help!!!!
the cpu and memory allocations are not related to the size of the image on your hard drive. It completly related to the number of pixels you have. if you have a completely white image of 20,000 pixel by 20,000 pixel, the size of this file on your hard drive can be 6 MG but when you load it into memory with Magick.net it will be gigabytes. So first you have to see what size(in pixels) are the images and then we can judge about the performance.
Then you can use these approaches to improve the performance:
once you load the image into memory you can write it on HDD with .mpc format and then you can load it into memory very fast. (if you need to load couple of times your images)
Use Magikc.net q8 instead of q16
if you can run the command on a parallel loop then Magick.net version 7 can run almost 4 times faster.
and as the other answer is saying you have to dispose your image when it has been done.
If you convert images in a loop then disposing each image after conversion is done it might help. Use
Image.Dispose();
method in order to free unmanaged memory resources used by images.

difference between image bitmap and fromFile methods in c#

I found out that there are two ways to read the image info using default c# library. One of them is
System.Drawing.Image image = new Bitmap("file..path");
Another one is :
Image image = Image.FromFile("file..path");
May anyone tell me which one will run faster if I need to read a lot of images(nearly 100TB data).
I found out that there are two ways to read the image info
You know, if it is just the image info you are after then I wouldn't use either function as both load the entire image into memory from disk - a rather wasteful exercise of the computer's resources.
Instead you should just load the image file header whether it be EXIF; BITMAPINFOHEADER or other depending on the image format. There are ways to load such info via .NET (see links below).
Image headers
Apart from RAW image file formats (not necessarily that which is output from SLR cameras), most image file formats have a header that can be loaded prior to loading the image raster data into memory from disk. In fact it is a generally a requirement that the header is read first because otherwise you would not know how much memory to allocate prior to loading the image.
How wide is it?
How tall?
How many bits per pixel (colour depth)?
...and so forth. These are all answered by reading the image file header first. As the name suggests, information about the image is generally near the start of the file. Exact formats and layout depends on the file format in question. See BMP; PNG resources for more info.
Here's some suggestions on loading image headers
Obtain image width and height without loading image in .NET?
Getting image dimensions without reading the entire file
Bitmap Storage

Opening huge multi page tiff file as bitmap

I am trying to open big tiff file as bitmap and display into a picture box control in c# frame work 3.5. I am not able to allocate huge memory to int array. How to allocate huge memory.
We used BitMiracle and opened each tile and combined it into one image
BitMiracle ReadRGBATile the Image quality is not good

c#: How to convert very large bitmaps

I am currently dealing with very large bitmaps (32bit, > 1GB) close to the bitmap size limit (~ 32 kpx squared). For file storage reasons, I would like to convert these bitmaps into more suitable formats, e.g. jpg or png. But I have to convert them without loading the whole bitmap into memory at once, since I will get a out-of-memory error.
Is there any way how to convert the bitmap in parts, e.g. linewise, blockwise or bandwise? At the moment, I am using Photoshop for saving, but this is not an option at a later stage of the project anymore.
Thanks for the help,
Max

resize picture c# for web

Goal:
I have lots of pictures in many sizes (both dimensions and file size)
I'd like to convert these files twice:
thumbnail-size pictures
pictures that will look OK on a web page and will be as close to a full screen as possible... and keeping the file size under 500KB.
HTML Questions:
A. What is the best file format to use (jpg, png or other) ?
B. What is the best configuration for web ... as small as possible file size with reasonable quality?
C# Questions
A Is there a good way to achieve this conversion using C# code (if yes, how)?
Try the code in this small C# app for resizing and compressing the graphics. I have reused this code for use in an ASP.NET site without too much work, hopefully you can make use of it. You can run the app to check quality fits your needs etc.
http://blog.bombdefused.com/2010/08/bulk-image-optimizer-in-c-full-source.html
You can pass the image twice, specifying dimensions for a thumbnail, and then again for your display image. It can handle multiple formats (jpg, png, bmp, tiff, gif), and reduce file size significantly without loosing noticeable quality.
On .jpg vs .png, generally jpg is better as you will get a smaller file size than with png. I've generally used this code passing a quality of 90%, which reduces file size significantly, but still looks perfect.
I think PNG is better format for WEB than JPEG that always uses lossy JPG compression, but its degree is selectable, for higher quality and larger files, or lower quality and smaller files. PNG uses ZIP compression which is lossless, and slightly more effective than LZW (slightly smaller files).
In C# you can use System.Drawing namespace types to load, resize and convert mages. This namespace wraps GDI+ API.
A. For graphics I would use png and for fotos jpg.
B. Configuration?
C. There are tons of post that explain that:
http://www.codeproject.com/KB/GDI-plus/imgresizoutperfgdiplus.aspx
Resizing an Image without losing any quality

Categories