Hi, i have these two methods which work as designed, but the original image still shows behind... These methods are to resize an image, one is increase and one is decrease size.

Upon smallerByToolStripMenuItem_Click the image is resized, but the original shows behind it.

Upon largerByToolStripMenuItem_Click the image is also resized, but the bounds of that image stay the same, but i thought the width & hiehgt are behind re-created??...

gr.DrawImage(loadedImage, new System.Drawing.Rectangle(0, 0, (int)newWidth, (int)newHeight));
and they are calculated by new width = image.width/.9 (which make it bigger)??
800/.9 = 888.89....
Am i missing code, is it all wrong, is it the right "kind" of track?..

Anything would be great!. Thanks guys.

private void smallerByToolStripMenuItem_Click(object sender, EventArgs e)//---resize by 10%
        {
            double newWidth = (loadedImage.Width * .9);
            double newHeight = (loadedImage.Height * .9);

            using (Graphics gr = Graphics.FromImage(loadedImage))
            {
                gr.SmoothingMode = SmoothingMode.AntiAlias;
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                gr.DrawImage(loadedImage, new System.Drawing.Rectangle(0, 0, (int)newWidth, (int)newHeight));
            }
        }
//increase size by 10
        private void largerByToolStripMenuItem_Click(object sender, EventArgs e)
        {
            double newWidth = (loadedImage.Width / .9);
            double newHeight = (loadedImage.Height / .9);
            using (Graphics gr = Graphics.FromImage(loadedImage))
            {
                gr.SmoothingMode = SmoothingMode.AntiAlias;
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                gr.DrawImage(loadedImage, new System.Drawing.Rectangle(0, 0, (int)newWidth, (int)newHeight));
            }
        }

Main goal: Get older image to dissapear
Second goal: Any tips on increasing image size.

I'm using a single method for resizing images in both directions.
It returns a BitMap object that you can use on wherever you like.

public BitMap ResizeBitMap(BitMap source, Size newSize)
{
   //Get the source bitmap
   BitMap bm_source = source;

   //Create a bitmap for the result
   BitMap bm_dest = new BitMap(newSize.Width, newSize.Height);

   //Create a Graphics object for the resulting BitMap
   Graphics gr_dest = Graphics.FromImage(bm_dest);

   //Copy the source image into the destination bitmap
   gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1);

   //Return the result
   return bm_dest;
}

Here is one way of how you can use it:

private void someMethod()
{
   BitMap resizedImage = ResizeBitMap(loadedImage, new Size(loadedImage.Width * .9, loadedImage.Height * .9));

   //Stick that resized bitmap anywhere you like it.
   //For example a PictureBox
   pictureBox1.Image = resizedImage;
}

@Walid86 your routines are drawing the image on to itself.

This Graphics gr = Graphics.FromImage(loadedImage) gets a Graphics object that draws on to the image.

This gr.DrawImage(loadedImage, ...); draws loadedImage on to that Graphics object (i.e. loadedImage).

Neither of these operations change the size of loadedImage so its bounds remain the same.

Try resizing like this.
(Similar to Oxiegen's resize solution but lets the Bitmap constructor do the hard work.)

double newWidth = (loadedImage.Width * .9);
double newHeight = (loadedImage.Height * .9);

loadedImage = new Bitmap(loadedImage, newWidth, newHeight);

thanks, i have got it resizing correctly, but the original image is still behind it... I remember seeing a post about needing to image.dispose to remove the older image, but its not working for me?.

How do i get the project to remove the old image upon resizing a copy of it?.

thanks.

Post your code again so we can see how you are doing it now.

private void smallerBy10ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            double newWidth = (loadedImage1.Width * .9);
            double newHeight = (loadedImage1.Height * .9);

            using (Graphics gr = Graphics.FromImage(loadedImage1))
            {
                gr.SmoothingMode = SmoothingMode.AntiAlias;
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                gr.DrawImage(loadedImage1, new System.Drawing.Rectangle(0, 0, (int)newWidth, (int)newHeight));
            }

        }

This code makes the bitmap smaller by 10% but once the image is created, you can still see the old image in the background...

i know we are meant to dispose of the old image, just not sure where....

thanks.

OK, you do not seem to have changed the code at all!

You can not change the size of an existing Image object.
You must create a new image with the required size and use this instead of the original image.
The Bitmap constructor does this for you as I showed in my previous post.

I tried using the code provided, but it caused an error, and i cannot see where or why. I am getting an error on this line

Bitmap resizedImage = ResizeBitMap(loadedImage1, new Size(loadedImage1 * .9, loadedImage1.Height * .9));

Error 1 The best overloaded method match for 'iDMS.propertyBrochreFront.ResizeBitMap(System.Drawing.Bitmap, System.Drawing.Size)' has some invalid arguments

my two methods are as follows...

user clicks button to resize...

private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            Bitmap resizedImage = ResizeBitMap(loadedImage1, new Size(loadedImage1 * .9, loadedImage1.Height * .9));
            //Stick that resized bitmap anywhere you like it.
            //For example a PictureBox
            pictureBox1.Image = resizedImage;
        }

method as provided to do the resizing..

public Bitmap ResizeBitMap(Bitmap source, Size newSize)
        {
            //Get the source bitmap
            Bitmap bm_source = source;

            //Create a bitmap for the result
            Bitmap bm_dest = new Bitmap(newSize.Width, newSize.Height);

            //Create a Graphics object for the resulting BitMap
            Graphics gr_dest = Graphics.FromImage(bm_dest);

            //Copy the source image into the destination bitmap
            gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1);

            //Return the result
            return bm_dest;
        }

If the image is in the Image property of an PictureBox then use this to resize it.
First set the PictureBox.AutoSize property to true (if it is not already).
Then use:

var image = pictureBox1.Image;
var newWidth = (image.Width * .9);
var newHeight = (image.Height * .9);
pictureBox1.Image = new Bitmap(image, (int)newWidth, (int)newHeight);

[Edit]
The ResizeBitMap method is pointless as the same thing is accomplished with the Bitmap constructor.

Note: If you shrink a big image by a large amount and then enlarge it you will not get back the original image.
To do this you need to store the original image and keep track of is magnification and resize the original each time.

I tried using the code provided, but it caused an error, and i cannot see where or why.

The error you are getting is probably because Size needs int variables and you are passing variables of type double.

yeah, i thought it had something to do with that, but there was always an error whereever i put (int) to make it int not double.. so i did more research and found another resize method, similar to whats posted on here, and i got it working.. code is below:

public Image bmResize(Image img, int percentage)
        {
            //get the height and width of the image
            int originalW = img.Width;
            int originalH = img.Height;

            //get the new size based on the percentage change
            int resizedW = (int)(originalW * percentage/100);
            int resizedH = (int)(originalH * percentage/100);

            //create a new Bitmap the size of the new image
            Bitmap bmp = new Bitmap(resizedW, resizedH);
            //create a new graphic from the Bitmap
            Graphics graphic = Graphics.FromImage((Image)bmp);
            graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //draw the newly resized image
            graphic.DrawImage(img, 0, 0, resizedW, resizedH);
            //dispose and free up the resources
            graphic.Dispose();
            //return the image
            return (Image)bmp;
        }

and the user clicks on image to resize blah blah...

private void smallerBy10ToolStripMenuItem_Click(object sender, EventArgs e)
        {  
            Bitmap resizedImage = (Bitmap)bmResize(loadedImage1, 90);
            pictureBox1.Image = resizedImage;

        }

All works fine, but i think this can only be called once, as clicking "resize" more than once has no difference, (unless its happening and i can't see it) and the old image is still behind it.. but i only got this working, so i'll take some more time to see whats missing..

thanks guys!.

The lack of size change on two clicks is because 90 is a percentage of the original image and does not take in to account the first re-size.

You might be able to use pictureBox1.Image.Size to calculate what the new size should be based on the original image.

Image image = loadedImage1;
            var newWidth = (image.Width / .8);
            var newHeight = (image.Height / .8);
            loadedImage1 = new Bitmap(image, (int)newWidth, (int)newHeight);

this code works, replaces the bitmap with the new size and puts it in picturebox1, but the original image is still the original size, infront of the newly created smaller bitmap, and the smaller bitmap is not moveable as it is prior to making it smaller..


This is how i put the image onto the picturebox..

// load image to image object
                loadedImage1 = Image.FromFile(openFD.FileName);
                // set initial rectangle bounds for image
                imageBounds1 = new System.Drawing.Rectangle(new Point(), loadedImage1.Size);
                // refresh the picture box to show new image
                pictureBox1.Refresh();

If you set pictureBox1.Image = Nothing before setting the resized image, the picturebox will be cleared and the original image will not be there anymore.
Only the resized one.

double newWidth = (loadedImage1.Width * .9);
            double newHeight = (loadedImage1.Height * .9);
            pictureBox1.Image = null;
            loadedImage1 = new Bitmap(loadedImage1, (int)newWidth, (int)newHeight);

if that's what you mean, i already tried, but with that i get the new image, at the same size, but quality is decreasing...

image is movable, but not as required..

But then i had my hopes up and thought if i use the original method and then clear the loadedImage?.. it worked, but as loadedimage is now null, it is not moveable.. but at the new size..

double newWidth = (loadedImage1.Width * .9);
            double newHeight = (loadedImage1.Height * .9);

            loadedImage1 = new Bitmap(loadedImage1, (int)newWidth, (int)newHeight);
            pictureBox1.Image = loadedImage1;
            loadedImage1 = null;  <--- clear the image on top of the good image...

i'll have another go tomorrow.. 11:55pm, second coffee down, bed time..
....glad no code is involved! lol

SOLVED!!

I finally figured my issue out. if anyone is interested how or why please read on...

so i load the image into the picturebox using...

loadedImage1 = Image.FromFile(openFD.FileName);
                // set initial rectangle bounds for image
                imageBounds1 = new System.Drawing.Rectangle(new Point(), loadedImage1.Size);
                // refresh the picture box to show new image
                pictureBox1.Refresh();

and i was trying to resize like..

private void smallerBy10ToolStripMenuItem_Click(object sender, EventArgs e) //pb1 smaller
        {

            double newWidth = (loadedImage1.Width);
            double newHeight = (loadedImage1.Height);
            double scalingFactor = .9;

            loadedImage1 = CreateNewBitmapFrom(loadedImage1, (int)(loadedImage1.Width * scalingFactor), (int)(loadedImage1.Height * scalingFactor));

            pictureBox1.Invalidate();
           
        }

with..

public static Bitmap CreateNewBitmapFrom(Image source, int targetWidth, int targetHeight)
        {
            var newBmp = new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppArgb);
            using (var g = Graphics.FromImage(newBmp)) g.DrawImage(source, 0, 0, newBmp.Width, newBmp.Height);
            return newBmp;
        }

and it was working, but the image stayed the same, and if i put pictureBox1.image = loadedimage i get the image but not the right size/quality..

so then i thought, imagebounds has to have something to do with this.. so i then tried to resize imagebounds with no luck and alot of time wasted..

after reading alot on the paint method and trying to override it, and all that fancy stuff, i understood that its always called.. soo.. if it's being called, it MUST be working, but my image is the same size..

Then i thought, what is missing.. **looks at code**

and it hit me....

// set initial rectangle bounds for image
            imageBounds1 = new System.Drawing.Rectangle(new Point(), loadedImage1.Size);

insert a new image bounds, and get the NEW imagebounds which is the new size, then call paint..

and it worked perfectly..!!!

double newWidth = (loadedImage1.Width);
            double newHeight = (loadedImage1.Height);
            double scalingFactor = .9;

            loadedImage1 = CreateNewBitmapFrom(loadedImage1, (int)(loadedImage1.Width * scalingFactor), (int)(loadedImage1.Height * scalingFactor));

            // set rectangle bounds for image
            imageBounds1 = new System.Drawing.Rectangle(new Point(), loadedImage1.Size);

            pictureBox1.Invalidate();
private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
        // always paint the image on the box (after it is loaded)
            if (loadedImage1 != null)
                e.Graphics.DrawImage(loadedImage1, imageBounds1);
        }
loadedImage1 is painted with the new image
imagebounds are new bounds set by the resize part...
:D

THANK YOU to all who posted and tried to help, you guys saved me alot of code, and now understand alot more of C#...

regards,


Walid.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.