Enlarge in picture ?

Anonymous
2021-03-30T02:04:29.763+00:00

I have a picture I want to enlarge it 5x. I have the codes to make the picture 5x smaller. How can I reverse these codes.
So, how can I enlarge the picture with Pixel Replacement?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,099 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 110.8K Reputation points
    2021-04-02T09:00:12.347+00:00

    If you really want to zoom the image by repeating the pixels in loops, maybe with some colour replacement, then try this code too:

    Color WriteColor, ChangeColor;
    Bitmap FirstImage, LastImage;
    FirstImage = pictureBox1.Image as Bitmap ?? new Bitmap( pictureBox1.Image );
    int ImageWidth = FirstImage.Width;
    int ImageHeight = FirstImage.Height;
    const int Coefficient = 5;
    LastImage = new Bitmap( ImageWidth * Coefficient, ImageHeight * Coefficient );
    
    for( int x1 = 0, x2 = 0; x1 < ImageWidth; ++x1, x2 += Coefficient )
    {
        for( int y1 = 0, y2 = 0; y1 < ImageHeight; ++y1, y2 += Coefficient )
        {
            WriteColor = FirstImage.GetPixel( x1, y1 );
            ChangeColor = WriteColor;
            for( int x = x2; x < x2 + Coefficient; ++x )
            {
                for( int y = y2; y < y2 + Coefficient; ++y )
                {
                    LastImage.SetPixel( x, y, ChangeColor );
                }
            }
        }
    }
    pictureBox2.Image = LastImage;
    

3 additional answers

Sort by: Most helpful
  1. Viorel 110.8K Reputation points
    2021-03-30T05:04:34.1+00:00

    To obtain a zoomed image, having larger size:

    var new_image = new Bitmap( pictureBox1.Image, pictureBox1.Image.Width * 5, pictureBox1.Image.Height * 5 );
    pictureBox2.Image = new_image;
    

    To zoom and keep the size of original image (crop):

    using( var scaled_image = new Bitmap( pictureBox1.Image, pictureBox1.Image.Width * 5, pictureBox1.Image.Height * 5 ) )
    {
        var new_image = new Bitmap( pictureBox1.Image.Width, pictureBox1.Image.Height );
    
        using( var g = Graphics.FromImage( new_image ) )
        {
            g.DrawImage( scaled_image, Point.Empty );
        }
    
        pictureBox2.Image = new_image;
    }
    

  2. Castorix31 81,141 Reputation points
    2021-03-30T09:33:59.717+00:00

    ...and you can see the usual doc :

    How to: Crop and Scale Images


  3. Karen Payne MVP 35,016 Reputation points
    2021-04-01T23:43:37.987+00:00

    The following comes from the following GitHub repository but has issues.

    Here is the modified version in the following GitHub repository with full source.

    83854-custompicturebox.png

    .NET Core version

    Source

    83995-custompicturebox.png