splitting image

G

Guest

I need to load the screen size picture into PocketPC and show it to user.
Then I need to split this picture into array (let's say 10x10), shuffle it and show this shuffled picture to user again.

//Load Picture
inImage = new Bitmap (myFile);
Graphics tmpG = Graphics.FromImage(inImage);
tmpG.DrawImage(inImage, 0,0);
tmpG.Dispose();
tmpG = null;

//Split picture to array [10,10]
private Image [,] fieldImages;
fieldImages = new Image [10,10];
tmpSquare = this.ClientSize.Width/10;

for(int y = 0; y < 10 ; y++){
for (int x = 0; x < 10 ; x++){
Image tmpImage = new Bitmap(tmpSquare, tmpSquare);
Graphics smallG = Graphics.FromImage(tmpImage);
Rectangle tmpRect = new Rectangle (tmpSquare * x, tmpSquare * y, tmpSquare, tmpSquare);
smallG.DrawImage(inImage, 0,0, tmpRect, GraphicsUnit.Pixel);
fieldImages[x,y] = tmpImage;
tmpImage = null;
smallG.Dispose();
smallG = null;
}
}

It works, but terribly slowly. The main reason is creating (and disposing) new Image and new Graphics in for loop 10x10=100 times!
I want to ask is there a way to the same task faster? Can I split an image (take a piece of it without resizing), put it into array, and do not create new Graphics? May be there are proper API functions or other techniques? A piece of code would be welcome.
 
E

Ed Kaim [MSFT]

You should first start by pulling "Graphics smallG =
Graphics.FromImage(tmpImage);" out of the loop. That should speed up
performance significantly since you'll be making one call to this expensive
method instead of 100. You can also cut out the second redraw of the initial
image and the usage of the "tmp" bitmapwithin the loop.

I haven't tried compiling this code, but it should look something like this:

//Load Picture
inImage = new Bitmap (myFile);

//Split picture to array [10,10]
private Image [,] fieldImages;
fieldImages = new Image [10,10];
tmpSquare = this.ClientSize.Width/10;

Graphics smallG = Graphics.FromImage(tmpImage);

for(int y = 0; y < 10 ; y++){
for (int x = 0; x < 10 ; x++){
fieldImages[x,y] = new Bitmap(tmpSquare, tmpSquare);
Rectangle tmpRect = new Rectangle (tmpSquare * x, tmpSquare * y, tmpSquare,
tmpSquare);
smallG.DrawImage(inImage, 0,0, tmpRect, GraphicsUnit.Pixel);
}
}

sly2m said:
I need to load the screen size picture into PocketPC and show it to user.
Then I need to split this picture into array (let's say 10x10), shuffle it
and show this shuffled picture to user again.
//Load Picture
inImage = new Bitmap (myFile);
Graphics tmpG = Graphics.FromImage(inImage);
tmpG.DrawImage(inImage, 0,0);
tmpG.Dispose();
tmpG = null;

//Split picture to array [10,10]
private Image [,] fieldImages;
fieldImages = new Image [10,10];
tmpSquare = this.ClientSize.Width/10;

for(int y = 0; y < 10 ; y++){
for (int x = 0; x < 10 ; x++){
Image tmpImage = new Bitmap(tmpSquare, tmpSquare);
Graphics smallG = Graphics.FromImage(tmpImage);
Rectangle tmpRect = new Rectangle (tmpSquare * x, tmpSquare * y, tmpSquare, tmpSquare);
smallG.DrawImage(inImage, 0,0, tmpRect, GraphicsUnit.Pixel);
fieldImages[x,y] = tmpImage;
tmpImage = null;
smallG.Dispose();
smallG = null;
}
}

It works, but terribly slowly. The main reason is creating (and disposing)
new Image and new Graphics in for loop 10x10=100 times!
I want to ask is there a way to the same task faster? Can I split an image
(take a piece of it without resizing), put it into array, and do not create
new Graphics? May be there are proper API functions or other techniques? A
piece of code would be welcome.
 
G

Guest

No it is not working. If we use this code:

//Load Pictur
inImage = new Bitmap (myFile)

//Split picture to array [10,10
private Image [,] fieldImages
fieldImages = new Image [10,10]
tmpSquare = this.ClientSize.Width/10

Graphics smallG = Graphics.FromImage(tmpImage)

for(int y = 0; y < 10 ; y++)
for (int x = 0; x < 10 ; x++)
fieldImages[x,y] = new Bitmap(tmpSquare, tmpSquare)
Rectangle tmpRect = new Rectangle (tmpSquare * x, tmpSquare * y, tmpSquare
tmpSquare)
smallG.DrawImage(inImage, 0,0, tmpRect, GraphicsUnit.Pixel)



all items in array fieldImages[,] hold the LAST pices of the original picture, in our case it is the right bottom piec
In order to avoid it I HAVE to put creating of each graphics IN the loop
for(int y = 0; y < 10 ; y++)
for (int x = 0; x < 10 ; x++)
Graphics smallG = Graphics.FromImage(tmpImage)
..

May be there is another way to do this task? API functions or so...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top