views:

49

answers:

2

am using c#

am having a bitmap image like below

alt text

i want create a repeated image like below in horizontal position to get repeted continous image for some given width. i meant i like to draw repeated image like below from the above single bitmap (In simple words,in html we can have a image and set repeat X to get the repeated image.like that) how i can do this in c#.

alt text

so that i can draw a new bitmap in my application. How to do this.?

A: 

you don't need to create other bitmaps. it's a matter of drawing bitmap. in the place you darw the bitmap use drawImage method few times and increment the X position of the bitmap by its width. say 16 is the width of your image. make sure that bitmap has been initialized.

private void Form1_Paint(object sender, PaintEventArgs e)
{

    e.Graphics.DrawImage(bmp,x,y);
    e.Graphics.DrawImage(bmp,x+16,y);
    e.Graphics.DrawImage(bmp,x+32,y);
}
Arseny
@Arseny i tried this, but its not working, the lastly created image in graph, when i try to draw the bmp its not drawing the last bmp, Do u want me to draw bmp or graph ??? can u elobrate me more
deep
@deep Graphics is a class for drawing in .NET so you can draw images via this class like in the example. One thing to mention is that you need to know where to call this routine. I wound recomend to handle Paint event of your control and use the Graphics from PaintEventArgs argument instead of creating it.
Arseny
its not working Arseny, i tried to bind the new one, but am not getting the repeated image?
deep
System.Drawing.Bitmap bmp = (Bitmap)Bitmap.FromFile(@"C:\\untitled.JPG");Graphics graph = Graphics.FromImage(bmp);graph.DrawImage(bmp, 52, 66);graph.DrawImage(bmp, 52 + 52, 66);graph.DrawImage(bmp, 52 + 104, 66);MemoryStream strm = new MemoryStream();bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Png);BitmapImage bmpImage = new BitmapImage();bmpImage.BeginInit();strm.Seek(0, SeekOrigin.Begin);bmpImage.StreamSource = strm;bmpImage.EndInit();image1.Source = bmpImage;am using like above, but am getting only the single image.. whats the problem?
deep
@arseny ya what you said is correct, when i check in winforms its working, but am using WPF, i tried to change it in wpf, but i cannot, if u have any idea plz share it, how to do with wpf?
deep
A: 

You can do it like this:

Bitmap myImage = new Bitmap(50, 50); //assuming you want you image to be 50,50
Bitmap originalImage = new Bitmap("myPngSource.png"); //original image to copy

using (Graphics g = Graphics.FromImage(myImage))
{
     g.DrawImage(originalImage, new Rectangle(0, 0, originalImage.Width, originalImage.Height));
}

MemoryStream ms = new MemoryStream();
myImage.Save(ms, ImageFormat.Png);

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();

MyImageControl.Source = bi;

Or something like that, this is untested, and I just ripped it out of a little utility app I made a while ago. I hope it helps... You just need to change the width of the final image and do a loop over the g.DrawImage call incrementing the second parameter by the width of the originalImage. (i.e. if you want 5 repeats, do a for loop 5 times)

HTH --Mark

Mark