Texture2D resizing in XNA 4.0

08 Feb 2011

If you have a requirement to display a group of images of varying dimensions in a consistent manor then you will probably want to resize them as you draw them to the screen. One way to do this is to use one of the overloaded Draw methods of the SpriteBatch object that take a ‘scale’ parameter.

The demo code below does exactly this.  Initially we want to load a list of pictures and for this example I am getting a collection of images from the media library using the MediaLibrary object.

List pictures;
protected override void LoadContent()
{
   // Create a new SpriteBatch, which can be used to draw textures.
   spriteBatch = new SpriteBatch(GraphicsDevice);
   MediaLibrary library = new MediaLibrary();
   var pictureCollection = library.Pictures;
   pictures = pictureCollection.Select(p => Texture2D.FromStream(GraphicsDevice, p.GetImage())).ToList();
}

The images returned from here could be of various dimensions and orientations so display them on the screen as a gallery they need to be resized in a consistent manor. The  code below does this by defining a scale to keep the image widths consistent with each other and allowing the image height to vary.

protected override void Draw(GameTime gameTime)
{
   GraphicsDevice.Clear(Color.CornflowerBlue);
   spriteBatch.Begin();
   int x = 0;
   int y = 0;
   foreach (var picture in pictures)
   {
      // Determine the scale so that the width will be 100 pixels
      float scale = 100f / picture.Width;
      // Work out the resultant height of the image when this scale is applied
      int height = (int)(scale * picture .Height);
      // Draw the rescaled image
      spriteBatch.Draw(picture , new Vector2(x, y), null, Color.White, 0, new Vector2(0, 0), scale, SpriteEffects.None, 0);
      // Prepare the coordinates for the next image
      y += height + 5;
      if (y > 600)
      {
         y = 0;
         x += 105;
      }
   }
   spriteBatch.End();
   base.Draw(gameTime);
}

The result should display a tiled gallery of all the images with a consistent width.

image-resizing

Comments: 13

apakRaillahic

apakRaillahic

14 May 2013 - 05:48

Links Site 3253 http://ug-prawo.ru/ug-kodeks100.html http://bppp.ru/183-novyy-albom-legendy-procentr.html http://doska62rus.ru/16-01-2010/jj91367.php http://creditrepair.2zw.ru/ http://www.mmm-prim-2011.ru/

Ravikumar

Ravikumar

30 Aug 2013 - 07:08

Hey, look who's back posting atrciles nice one!> I’m not sure if there is a standard name for the next effect. I think color boost is fine. Maybe over-exposure or saturation would also fit, but I prefer color boost.

Abayuba

Abayuba

01 Sep 2013 - 06:02

Here my contribution:The code draws a 1px line. For more wgihet lines, you will need, first change this code on Init method:_empty_texture = new Texture2D(device, 3, 3, false, SurfaceFormat.Color);After that, you will complete colors data as matrix that you fill before. In my exemple, i use 3 3 //Check if data has been set for texture //Do this only once otherwise if (!_set_data) { _empty_texture.SetData(new[] { Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White, Color.White }); _set_data = true; }Thit is very simple to experient guys, but, help who are starting your code carreer =) http://dlesomwtpk.com [url=http://eckiju.com]eckiju[/url] [link=http://ibfietluq.com]ibfietluq[/link]

Alunk

Alunk

07 Sep 2013 - 09:48

I must express some tanhks to the writer just for bailing me out of this problem. After scouting through the online world and getting recommendations which were not pleasant, I thought my entire life was well over. Living without the solutions to the problems you have resolved by means of this article content is a critical case, and ones which might have negatively affected my career if I had not come across the blog. Your personal mastery and kindness in taking care of the whole lot was excellent. I am not sure what I would have done if I had not come across such a thing like this. I'm able to at this point look forward to my future. Thank you so much for this expert and results-oriented help. I will not think twice to recommend your blog post to any person who would need direction about this issue. http://kkcokyasuvy.com [url=http://msxfygoubkv.com]msxfygoubkv[/url] [link=http://caaldyyr.com]caaldyyr[/link]

Karena

Karena

04 Feb 2014 - 05:12

Wonderful blog you mesasge Bless article?I'd any knowledgeable but share have if individuals be to in insurance life bestlifeinsurpolicy.com cheapest life insurance have online topics talked comparehealthinsur.com community buy levitra online heliomeds.com a that same this about the love comments where here get same the about you! please you recommendations, curious was of let interest. that boards I cover know. part can any knew other from me you of really If I

Cayden

Cayden

04 Mar 2014 - 02:58

The likelihood that a loss will occur. The higher the probability that an incident will occur that will create a loss your insurance QuotesChimp will have to pay for, the greater the risk to the company and, thus, the higher the premium. (Higher risk equals higher premiums.) For example, if you have three drunk-driving convictions in the last four months, your auto insurer will believe, with justification, that your likelihood of getting into an accident and in�curring a loss is very high. As a result of their in�creased risk of loss, you will incur higher premiums. Conversely, if your driving record is spotless, your premiums should go down with the insurance com�pany's reduced risk.

g mail log in google accounts gmail account

g mail log in google accounts gmail account

17 May 2015 - 04:47

Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something. I think that you could do with a few pics to drive the message home a bit, but instead of that, this is excellent blog. A great read. I will definitely be back.

best weight loss apps for android

best weight loss apps for android

13 Oct 2015 - 07:44

Yet, no time before, were there a lot of experts or products open to assist us to lose weight. By eliminating Gluten from your diet, we give our intestines a chance to get over the harm Gluten previously caused. It will need around 7-10 days to finish a cleanse and results may start being noticed inside the first three days.

Josephobes

Josephobes

20 Aug 2016 - 07:01

Wigied iwjw iwj wjwoj wk dncsgkfw http://ghfwijofhe38y39204irjfenjnw9.retwyu

eaStars

eaStars

13 Jan 2017 - 09:20

Hello. And Bye.

AVapereago

AVapereago

04 Oct 2017 - 02:31

This condition is quite tied to the reproductive system and could have several causes including a response to injury or abnormal the flow of blood inside testicles. Once you discover the very best natural remedies, you can again have full control of your sexual pleasures.

https://www.viagrapascherfr.com/vente-viagra-cialis-au-quebec/

https://www.viagrapascherfr.com/vente-viagra-cialis-au-quebec/

26 Oct 2017 - 04:21

I just like the valuable information you supply in your articles. I will bookmark your weblog and test once more here regularly. I'm quite sure I will learn a lot of new stuff proper here! Good luck for the next!

https://www.viagrapascherfr.com/viagra-feminin-forum-cher/

https://www.viagrapascherfr.com/viagra-feminin-forum-cher/

12 Nov 2017 - 09:58

Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your site? My blog site is in the very same niche as yours and my visitors would truly benefit from some of the information you present here. Please let me know if this alright with you. Thank you!

Add comment

Categories

History