Click here to Skip to main content
16,019,018 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I work on a game where is a map of dynamic height and width (for example 30x30). On one screen I want to display the map to user bigger (political map). Let's say I want to display it in 600x600.
I understand, I probably cannot use classic ways of .NET, because when I resize it, it is blurred. The only way to not have it blurred is to set InterpolationMode.NearestNeighbor. However is is "squared" when I use it.
I am just wondering about one thing -Is there a resize method, which can approximate the new pixels from the old one instead of blurring it?
Something like
exaxmple

I use System.Drawing, not WPF
Posted
Updated 21-May-11 22:45pm
v4

1 solution

No, but you can easily render such thing.

I'll explain how to do it if you tag your question: what do you use: WPF or System.Drawing (with System.Windows.Forms or not)? Don't just answer my question, tag it! (You want to attract other experts to your question, so always tag very accurately.)

[EDIT: providing more detail after OP's clarifications]

With System.Windows.Forms and System.Drawing, you first need to get an array of pixels. This is done using System.Drawing.Bitmap.LockBits. You can use it with either System.Runtime.InteropServices.Marshal.Copy or pointers in unsafe mode. You can find the code sample using Marshal here:
http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx[^].

Now, you scale it by an integer factor N (only integer). It means each pixel should be represented by a square N×N pixel or the uniform color. You need to render all those squares side by side &msash; as many as pixels in the original picture. You can do it using two ways:


  1. Use the same code sample as before and see how to write to a bitmap using LockBits and Marshal. The main difference is: you need to create another bitmap for rendering, N time bigger and set all pixels in it using the pixel array read from the original bitmap
  2. You can create a new bitmap N times bigger, create an instance of it System.Drawing.Graphics using the static method System.Drawing.Graphics.FromImage and render all those rectangles using this instance and the instance method System.Drawing.Graphics.FillRectangle.


Hard to say which method will be faster; it may also depends on your scaling factor. Please experiment. Would be nice if you share your results.

[EDIT]
Scaling via re-sampling is completely wrong approach anyway.

You need to use vector graphics and nothing else. There are several options: using your own data layer (collection of vector primitive, scale-free, scalable) and rendering on the fly for each scale, rendering based on metafile. I don't really want to discuss this because WPF beats all. It already has fully-fledged vector graphics with objects of any complexity, design-time or run-time, fully implemented scaling and more. Almost no low-level programming. No rendering at all. I would not even play with the idea of using Forms. They are getting obsolete anyway, I hope.
[END EDIT]

—SA
 
Share this answer
 
v5
Comments
Member 2415147 20-May-11 4:50am    
Updated and tagged. I use System.Drawing not WPF
Sergey Alexandrovich Kryukov 20-May-11 23:38pm    
Wait a minute! You tag says "Forms". This is NOT WPF! Where is the truth?
(see my comment below as well)
--SA
Member 2415147 20-May-11 8:16am    
Could you please point me in the right direction?I don't necessarily need code. Just what should I do
Sergey Alexandrovich Kryukov 20-May-11 23:37pm    
I see. System.Drawing does not go well with WPF. There is a special WPF interop namespace to work with System.Drawing... But you can switch to all-WPF as well, right?
--SA
Member 2415147 21-May-11 3:27am    
Actually not really. I need to have this working with Mono as well and there is no WPF support. So there is no way to do this in Winforms?
Btw. what is the main idea of doing this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900