This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

Resize multiple images

I am using Aurigma.GraphicsMill library to create different size of images from a single image.

My application is written using .NET C#.

The images are very large (around 30MB)


What I did was open image 3 times and resize and save it.
It takes long time for this process.

I am sure there must be an efficient way of doing this.

Can anyone point me in the right direction?

If you can provide a sample code that would be great.

Janaka
Come on guys, some one must have done this before ...
Hello,

To say something we should look through your code. Could you post your code sample here or submit case with it?
Here are two methods I've tried:

This first one is pretty standard, and I'm working with ccit4 compressed tiff files, so they're one bit images (black and white) and when I'm done with the resize, I have to change them back to one bit... now the ScaleToGrey method of resizing is particular to that, and depending on the quality you need you can adjust that to other methods of resizing for varied performance.. Also, intWidth contains the width I want in the end, I found out not too long ago that Aurigma will calculate that for me but haven't had a chance to change it yet, so ignore the aspect ratio calculation I'm doing...

        Dim tnfResize As Aurigma.GraphicsMill.Transforms.Resize

        Try
            bmpImage.Load(strFileName)
         Catch Ex As Exception
            Throw New Exception("Unable to load the requested image.", Ex)
        End Try
 
        tnfResize = New Aurigma.GraphicsMill.Transforms.Resize
 
        tnfResize.Width = intWidth
        tnfResize.Height = CInt(intWidth / (bmpImage.Width / bmpImage.Height))
 
        tnfResize.Priority = Threading.ThreadPriority.Highest
        tnfResize.ResizeMode = GraphicsMill.Transforms.ResizeMode.Fit
        tnfResize.InterpolationMode = GraphicsMill.Transforms.InterpolationMode.ScaleToGray
 
        Try
            tnfResize.ApplyTransform(bmpImage)
        Catch ex As Exception
            Throw New Exception("Error resizing image to " & tnfResize.Width & " width.")
        End Try
 
        Try
            bmpImage.ColorManagement.ConvertToIndexed(1, GraphicsMill.ColorPaletteType.GrayScale, Nothing)
        Catch Ex As Exception
            Throw New Exception("Error adapting color index to 1bpp.", Ex)
        End Try
 
        tnfResize.Dispose()
        tnfResize = Nothing



Here's a second method which usuaully seems to work better.. from my understanding its because the image is read and resized in one pass or there's less memory used or something like that?

But i think the usage of this syntax may be less flexible (in conversion quality etc..) so you'd have to try and see if it worked better for what you're doing.. the nice thing is its easy to try since its not hard to code up 🙂


        Try
            bmpImage.LoadThumbnail(strFileName, intWidth, 0)
        Catch Ex As Exception
            Throw New Exception("Unable to load the requested image.", Ex)
        End Try


The images I work with are probably 2500x3000 pixels or so and trying to resize them to about 50% is too expensive to use on a server, so i've actually removed the code from the server.. (it was a dual core P4 2.8 Ghz) and with it was probably taking about one second per resize (with 35 users that pegged the CPU)

Now I don't know if there's another way to do it that is more efficient? I'd love to know if that were the case 🙂 but my images are only 75K-250K, so with a 30MB image I'd imagine it'd be slow... ?
Let me clarify things little bit more …

My application is written using .NET C#. This is a normal windows application that does the image creation for the web site.


From an original image (around 30MB)
I have to create
A3, A4, and A5 images
Thumbnail (130x130, 72dpi)
Enlargement (389x265, 72dpi) with water mark
Composite image (500x500, 150dpi)

I have to process close to 15000 images (from our old website).

The way I did this was open original image, resize it, save it.
Did that 6 times to create those 6 images.

I found that the process was very slow. It would take ages to process them all.


I have attached the file with the functions I used to for this task.

BuildHiResImages – function is used to create A3, A4, and A5 images from the original image

BuildBasicImages - used to create thumbnail, Enlargement and Composite images, from the original image

when creating these images (BuildBasicImages) I have to convert the color mode to SRGB, for better viewing through web

Attachments

Jdelagarza, thanks for your post.


I am doing something similar to what you have posted.

My problem is I am doing it 6 times to create 6 different images.
I wanted to improve that.
Hello,

Your source code is very good but I can advise the following:
  • You should load source image once only;
  • Color management is very time expensive operation, you should apply it once;
  • You should create source image for thumbnail, enlargement and composite image of reduced size from source image because of resizing of huge image works long time. And then create your small images from this interim source image. I have defined A4 image as interim image of reduced size in my code sample.

    Here is updated code sample:
    System.String strSrcPath = @"d:/images/test/";
    System.String strDestPath = @"d:/temp/resize";
    	
    Aurigma.GraphicsMill.Drawing.Font font = new Aurigma.GraphicsMill.Drawing.Font("Arial", 20);
    Aurigma.GraphicsMill.Drawing.SolidBrush brush = new Aurigma.GraphicsMill.Drawing.SolidBrush(Aurigma.GraphicsMill.RgbColor.Red);
    	
    Aurigma.GraphicsMill.Transforms.Resize resize = new Aurigma.GraphicsMill.Transforms.Resize();
    resize.InterpolationMode = Aurigma.GraphicsMill.Transforms.InterpolationMode.HighQuality;
    resize.ResizeMode = Aurigma.GraphicsMill.Transforms.ResizeMode.Fit;
    	
    // Load source image once
    Aurigma.GraphicsMill.Bitmap srcBitmap = new Aurigma.GraphicsMill.Bitmap(strSrcPath + "pict0001.jpg");
    srcBitmap.HorizontalResolution = 72.0f;
    srcBitmap.VerticalResolution = 72.0f;
    	
    // A3
    using(Aurigma.GraphicsMill.Bitmap a3Bitmap = new Aurigma.GraphicsMill.Bitmap())
    {
    	resize.Width = 846;
    	resize.Height = 1188;
    	resize.ApplyTransform(srcBitmap, a3Bitmap);
    	a3Bitmap.Save(strDestPath + "a3.jpg");
    }
    	
    // A4
    Aurigma.GraphicsMill.Bitmap a4Bitmap = new Aurigma.GraphicsMill.Bitmap();
    resize.Width = 594;
    resize.Height = 846;
    resize.ApplyTransform(srcBitmap, a4Bitmap);
    a4Bitmap.Save(strDestPath + "a4.jpg");
    	
    // A5
    using(Aurigma.GraphicsMill.Bitmap a5Bitmap = new Aurigma.GraphicsMill.Bitmap())
    {
    	resize.Width = 423;
    	resize.Height = 594;
    	resize.ApplyTransform(srcBitmap, a5Bitmap);
    	a5Bitmap.Save(strDestPath + "a5.jpg");
    }
    	
    // Source bitmap for thumbnail, enlargement and composite image.
    Aurigma.GraphicsMill.Bitmap srcThumbnailBitmap = a4Bitmap;
    	
    // Convert color space to RGB once
    if(srcThumbnailBitmap.ColorSpace != Aurigma.GraphicsMill.ColorSpace.Rgb)
    {
    	srcThumbnailBitmap.ColorManagement.RgbColorProfile = Aurigma.GraphicsMill.ColorProfile.FromSrgb();
    	srcThumbnailBitmap.ColorManagement.ColorManagementEnabled = true;
    	srcThumbnailBitmap.ColorManagement.ConvertToContinuous(Aurigma.GraphicsMill.ColorSpace.Rgb, false, srcThumbnailBitmap.IsExtended);
    }
    	
    // Thumbnail
    using(Aurigma.GraphicsMill.Bitmap thumbnail = new Aurigma.GraphicsMill.Bitmap())
    {
    	resize.Width = 130;
    	resize.Height = 130;
    	resize.ApplyTransform(srcThumbnailBitmap, thumbnail);
    	thumbnail.HorizontalResolution = 72.0f;
    	thumbnail.VerticalResolution = 72.0f;
    	thumbnail.Save(strDestPath + "thumbnail.jpg");
    }
    	
    // Enlargement
    using(Aurigma.GraphicsMill.Bitmap enlargement = new Aurigma.GraphicsMill.Bitmap())
    {
    	resize.Width = 389;
    	resize.Height = 265;
    	resize.ApplyTransform(srcThumbnailBitmap, enlargement);
    	enlargement.HorizontalResolution = 72.0f;
    	enlargement.VerticalResolution = 72.0f;
    
    	using(Aurigma.GraphicsMill.Drawing.GdiGraphics gr = enlargement.GetGdiGraphics())
    	{
    		gr.DrawString("Aurigma Inc.", font, brush, 0, 0);
    	}
    
    	enlargement.Save(strDestPath + "enlargement.jpg");
    }
    	
    // Composite image
    using(Aurigma.GraphicsMill.Bitmap composite = new Aurigma.GraphicsMill.Bitmap())
    {
    	resize.Width = 500;
    	resize.Height = 500;
    	resize.ApplyTransform(srcThumbnailBitmap, composite);
    	composite.HorizontalResolution = 150.0f;
    	composite.VerticalResolution = 150.0f;
    	composite.Save(strDestPath + "composite.jpg");
    }
    
  • Dmitry thanks for your help.

    Your suggestions really improve the program.

    Janaka
    I have come across a small problem regarding the code.
    The original image contains metadata, but the resize images do not have them.

    How do we get the metadata to the new images we are creating?
    Hello,

    If you need to preserve EXIF/IPTC metadata, you should load it from source image before resize and save it along with resized images. Bitmap.Load doesn't support metadata handling, so you should use some reader instead of this. You can read more information in the articles: "Loading EXIF and IPTC Metadata" and "Saving EXIF and IPTC Metadata".
    I used the following code to resize the images with metadata.
    After resizing I could not open the image.

    The error was unexpected end-of-file was encountered.
    Any thoughts why it is happening?


    Janaka
    ----------------------------------------------------------------------------------
    
    Aurigma.GraphicsMill.Transforms.Resize resize               = new Aurigma.GraphicsMill.Transforms.Resize(); 
                resize.InterpolationMode                                    = Aurigma.GraphicsMill.Transforms.InterpolationMode.HighQuality; 
                resize.ResizeMode                                           = Aurigma.GraphicsMill.Transforms.ResizeMode.Fit;
    
                Aurigma.GraphicsMill.Codecs.ExifDictionary exif             = null;
                Aurigma.GraphicsMill.Codecs.IptcDictionary iptc             = null;
    
                Aurigma.GraphicsMill.Codecs.JpegEncoderOptions encoderOptions = 
                                    new Aurigma.GraphicsMill.Codecs.JpegEncoderOptions(70,false);
    
    
                using (Aurigma.GraphicsMill.Codecs.JpegReader jpegReader    = new Aurigma.GraphicsMill.Codecs.JpegReader(strSourceFileName))
                {
    
                    encoderOptions.Iptc                                         = jpegReader.Iptc;
                    encoderOptions.Exif                                         = jpegReader.Exif;
    
                }
                            
    
                Aurigma.GraphicsMill.Bitmap srcBitmap                       = new Aurigma.GraphicsMill.Bitmap(strSourceFileName);
    
    
                
                float size                                                  = ((float)srcBitmap.BitsPerPixel*srcBitmap.Width*srcBitmap.Height)/8388608;
                srcBitmap.Save(strDest + strA3, encoderOptions);
    
    

    I have tried the sample code in page


    http://www.aurigma.com/docs/gm/SavingEXIFandIPTCMetadata.htm



    The program runs with out any problem.



    I can also see the image created.



    But when I tried to open the new image using Photoshop to check the metadata written it gives the error, unexpected end-of-file was encountered.



    Can you advice how to solve this problem.





    Janaka
    Hello,

    Sorry for delayed answer. Please, post #5382.