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

H.B.
  • 166,899
  • 29
  • 327
  • 400
Chatra
  • 2,989
  • 7
  • 40
  • 73
  • *but the image is not displaying full.* What do you mean with that? Also, please show some code how its done – lokusking Jul 20 '16 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. – Chatra Jul 20 '16 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. – Clemens Jul 20 '16 at 17:48
  • @Clemens I want to save it – Chatra Jul 20 '16 at 17:50

1 Answers1

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

  • This is exactly what I am thinking. Can you please provide me with some code. – Chatra Jul 20 '16 at 17:27
  • Also you may want to use brushes (http://stackoverflow.com/questions/2675246/repeat-image-in-c-sharp), but for your problem, hand written code and brushes will act the same way. – Sergey.quixoticaxis.Ivanov Jul 20 '16 at 17:28
  • Yeap, wait a minute or two) – Sergey.quixoticaxis.Ivanov Jul 20 '16 at 17:28
  • 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. – Sergey.quixoticaxis.Ivanov Jul 20 '16 at 17:50
  • Thank you for the quick help – Chatra Jul 20 '16 at 18:00
  • Hi, For some reason, If height and width are same, then it is working fine. If both are different, it is cutting the image. Let say, you give multiplier as 2*1, it is cutting the image in bottom. if you give multiplier as 1*2, it is cutting image on right side. – Chatra Jul 21 '16 at 23:59
  • Hmm... I'm not sure about it. Just tried the code and it is not cutting anything. At least I don't see anything being cut. (both 1x2, 2x1, 2x3, etc.) – Sergey.quixoticaxis.Ivanov Jul 22 '16 at 00:23
  • Are you sure that picture is really being cut? Try saving it to disk and check with your eyes outside of WPF. Also did you copy the code or write it yourself? If the latter, you may want to check index variables, maybe you forgot to change I to j, k to l or something. – Sergey.quixoticaxis.Ivanov Jul 22 '16 at 00:25
  • I copied the code and works good for 1*1, 2*2 and 3*3 and not for different multipliers of height and width. I saved to disk and check outside WPF – Chatra Jul 22 '16 at 15:30
  • Then the problem is not in this code but somewhere else. As I mentioned before, I've checked it with 1920*1080 picture and multipliers 1x2, 2x1, 2x3, 3x2, 1x3, 3x1. – Sergey.quixoticaxis.Ivanov Jul 22 '16 at 17:00
  • I am able figure that the problem is some where else. I tried with new application and it is working. Thank you. – Chatra Jul 22 '16 at 17:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118045/discussion-between-sergey-quixoticaxis-ivanov-and-chatra). – Sergey.quixoticaxis.Ivanov Jul 22 '16 at 17:05
  • Hi, chat is blocked in my office. I will reply you once I went to home – Chatra Jul 22 '16 at 18:20