0

I have an image with Pixel Size 1024(width) x 1024(height) pixels. Lets say user wants to repeat that image 2 times to create another one. So, now the pixels would be 2048 x 2048. I am able to get the pixels in code but the image is not displaying full.

How can I do this using WPF and C# ?

More scenarios to the above 1) User wants to repeat only height, not width, then image would be 1024 x 2048 2) User wants to repeat only width 3 times, then image would be 3072 x 1024

4
  • but the image is not displaying full. What do you mean with that? Also, please show some code how its done Commented Jul 20, 2016 at 17:23
  • @lokusking Means, Instead of displaying 1024*2048 image, it is only displaying 1024*1024 image only. Sure, I will post the code. Please give me some time. Commented Jul 20, 2016 at 17:24
  • Do you only want to display the "repeated" image or do you also want to save it? Displaying only would easily be achieved with an ImageBrush. Commented Jul 20, 2016 at 17:48
  • @Clemens I want to save it Commented Jul 20, 2016 at 17:50

1 Answer 1

1

The straightforward way of doing what you want is to create a placeholder image with desired size: for example, if you have an image (Width, Height) you can create (n * Width, m * Height) and then copy the pixels.

If you need it, tell me, I'll provide you with some code.

private static Bitmap ResizeBitmap(Bitmap sourceBMP, Int32 widthMultiplier,
Int32 heightMultiplier)
    {
        var newWidth = sourceBMP.Width * widthMultiplier;
        var newHeight = sourceBMP.Height * heightMultiplier;
        var result = new Bitmap(newWidth, newHeight);
        using (Graphics g = Graphics.FromImage(result))
            g.DrawImage(sourceBMP, 0, 0, newWidth, newHeight);
        return result;
    }

    static void Main(string[] args)
    {
        var widthM = 2;
        var heightM = 2;

        var image = (Bitmap)Image.FromFile(@"E:\YOUR_IMAGE_HERE.png", true);

        var newImage = ResizeBitmap(image, widthM, heightM);
        for(var i=0; i<image.Width;++i)
            for(var j=0; j<image.Height;++j)
            {
                var pixelToCopy = image.GetPixel(i, j);
                for (var k = 0; k < widthM; ++k)
                    for (var l = 0; l < heightM; ++l)
                        newImage.SetPixel(k * image.Width + i,
                            l * image.Height + j,
                            pixelToCopy);
            }
        newImage.Save(@"E:\NEW_BIG_IMAGE_HERE.png", ImageFormat.Png);
    }
}

GetPixel and SetPixel are slow by the way. So you can probably adopt some unsafe code and rewrite loops with it. See example at MSDN

Sign up to request clarification or add additional context in comments.

13 Comments

This is exactly what I am thinking. Can you please provide me with some code.
Also you may want to use brushes (stackoverflow.com/questions/2675246/repeat-image-in-c-sharp), but for your problem, hand written code and brushes will act the same way.
Yeap, wait a minute or two)
I've added some code for bitmaps. I'm not too sure about this code being robust nor optimal. But if you don't need lightning speeds you may use it.
Thank you for the quick help
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.