Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C# how can I resize a jpeg image?

Tags:

c#

Using C# how can I resize a jpeg image? A code sample would be great.

like image 867
Steven W Avatar asked Jun 19 '10 14:06

Steven W


People also ask

What is C from used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C used nowadays?

There is at least one C compiler for almost every existent architecture. And nowadays, because of highly optimized binaries generated by modern compilers, it's not an easy task to improve on their output with hand written assembly.

Is C Programming good for beginners?

As I have said, C is a powerful, general-purpose programming language, and it's also a great language to learn when you start with programming. It gives you a lot more control over how your program uses memory, which is a tricky part but also very important if you want to become a better programmer.


1 Answers

Good free resize filter and example code.

http://code.google.com/p/zrlabs-yael/

    private void MakeResizedImage(string fromFile, string toFile, int maxWidth, int maxHeight)
    {
        int width;
        int height;

        using (System.Drawing.Image image = System.Drawing.Image.FromFile(fromFile))
        {
            DetermineResizeRatio(maxWidth, maxHeight, image.Width, image.Height, out width, out height);

            using (System.Drawing.Image thumbnailImage = image.GetThumbnailImage(width, height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
            {
                if (image.Width < thumbnailImage.Width && image.Height < thumbnailImage.Height)
                    File.Copy(fromFile, toFile);
                else
                {
                    ImageCodecInfo ec = GetCodecInfo();
                    EncoderParameters parms = new EncoderParameters(1);
                    parms.Param[0] = new EncoderParameter(Encoder.Compression, 40);

                    ZRLabs.Yael.BasicFilters.ResizeFilter rf = new ZRLabs.Yael.BasicFilters.ResizeFilter();
                    //rf.KeepAspectRatio = true;
                    rf.Height = height;
                    rf.Width = width;

                    System.Drawing.Image img = rf.ExecuteFilter(System.Drawing.Image.FromFile(fromFile));
                    img.Save(toFile, ec, parms);
                }
            }
        }
    }
like image 158
keithwarren7 Avatar answered Oct 23 '22 07:10

keithwarren7