Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:29:55+00:00 2026-06-15T16:29:55+00:00

I have a method that takes an image and resizes it and saves it

  • 0

I have a method that takes an image and resizes it and saves it preserving the exif information. What I want to do now is overlay a transparent PNG image on top of the image as a watermark. The size of the png will always be larger than any of the images I want to place it on. I would like to center it on top of the image preserving the watermark’s aspect ratio. Here is the code as I have it so far:

private static void ResizeImage(Image theImage, int newSize, string savePath, IEnumerable<PropertyItem> propertyItems)
{
    int width;
    int height;
    CalculateNewRatio(theImage.Width, theImage.Height, newSize, out width, out height);
    using (var b = new Bitmap(width, height))
    {
        using (var g = Graphics.FromImage(b))
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            using(var a = Image.FromFile("Watermark.png"))
            {
                g.DrawImage();  //What to do here?
            }
            g.DrawImage(theImage, new Rectangle(0, 0, width, height));

            var qualityParam = new EncoderParameter(Encoder.Quality, 80L);
            var codecs = ImageCodecInfo.GetImageEncoders();
            var jpegCodec = codecs.FirstOrDefault(t => t.MimeType == "image/jpeg");
            var encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;
            foreach(var item in propertyItems)
            {
                b.SetPropertyItem(item);
            }
            b.Save(savePath, jpegCodec, encoderParams);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:29:56+00:00Added an answer on June 15, 2026 at 4:29 pm

    I figured out the solution, the code is below. May not be the optimal code but it is fast and does what I need it to do which is take all JPG images in a directory and re-size them to full and thumb images for a photo gallery while overlaying a watermark on the image.

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Threading.Tasks;
    
    namespace ImageResize
    {
        internal class Program
        {
            private static readonly string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    
            private static void Main()
            {
                var strFiles = Directory.GetFiles(directory, "*.jpg");
    
                //Using parallel processing for performance
                Parallel.ForEach(strFiles, strFile =>
                                               {
                                                   using (var image = Image.FromFile(strFile, true))
                                                   {
                                                       var exif = image.PropertyItems;
                                                       var b = directory + "\\" + Path.GetFileNameWithoutExtension(strFile);
                                                       ResizeImage(image, 800, b + "_FULL.jpg", exif);
                                                       ResizeImage(image, 200, b + "_THUMB.jpg", exif);
                                                   }
                                                   File.Delete(strFile);
                                               });
            }
    
            private static void ResizeImage(Image theImage, int newSize, string savePath, IEnumerable<PropertyItem> propertyItems)
            {
                try
                {
                    int width;
                    int height;
                    CalculateNewRatio(theImage.Width, theImage.Height, newSize, out width, out height);
                    using (var b = new Bitmap(width, height))
                    {
                        using (var g = Graphics.FromImage(b))
                        {
                            g.SmoothingMode = SmoothingMode.AntiAlias;
                            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                            g.DrawImage(theImage, new Rectangle(0, 0, width, height));
    
                            //Using FileStream to avoid lock issues because of the parallel processing
                            using (var stream = new FileStream(directory + "\\Watermark.png", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                            {
                                using (var overLay = Image.FromStream(stream))
                                {
                                    stream.Close();
                                    int newWidth;
                                    int newHeight;
                                    CalculateNewRatio(overLay.Width, overLay.Height, height > width ? width : newSize, out newWidth, out newHeight);
                                    var x = (b.Width - newWidth) / 2;
                                    var y = (b.Height - newHeight) / 2;
                                    g.DrawImage(overLay, new Rectangle(x, y, newWidth, newHeight));
                                }
                            }
    
                            var qualityParam = new EncoderParameter(Encoder.Quality, 80L);
                            var codecs = ImageCodecInfo.GetImageEncoders();
                            var jpegCodec = codecs.FirstOrDefault(t => t.MimeType == "image/jpeg");
                            var encoderParams = new EncoderParameters(1);
                            encoderParams.Param[0] = qualityParam;
                            foreach (var item in propertyItems)
                            {
                                b.SetPropertyItem(item);
                            }
                            b.Save(savePath, jpegCodec, encoderParams);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
    
            private static void CalculateNewRatio(int width, int height, int desiredSize, out int newWidth, out int newHeight)
            {
                if ((width >= height && width > desiredSize) || (width <= height && height > desiredSize))
                {
                    if (width > height)
                    {
                        newWidth = desiredSize;
                        newHeight = height*newWidth/width;
                    }
                    else if (width < height)
                    {
                        newHeight = desiredSize;
                        newWidth = width*newHeight/height;
                    }
                    else
                    {
                        newWidth = desiredSize;
                        newHeight = desiredSize;
                    }
                }
                else
                {
                    newWidth = width;
                    newHeight = height;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I want to have a method that takes any kind of number, is
Okay, I have a method that takes the variables: an image, int x, int
I have a method that deals with an image. The method takes one image,
I have a method that takes a list of entities ( Class es) and
I have a method that takes an array of queries, and I need to
So I have a method that takes a tag and wraps the selected text
I am trying to have a method that takes in a username and will
All, I have a method that takes a date (YYYY-MM-DD H:M:S) from the database
I'm wondering how to go about testing this. I have a method that takes
I have a search method that takes in a user-entered string, splits it at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.