how to zoom an image (C#)

  • Thread starter Thread starter Kam
  • Start date Start date
K

Kam

Hi everyone,

Could anyone assist me on how to go about coding a pictureBox in C# to allow
the user to zoom a section of the image displayed..

any help, code sample, tutorial or links would be very much appreciated..

Thanks in advance,
Kam
 
I will take a try at this allthough I have never done it before.
First down load the following Sample :
Bitmap Copy

http://www.microsoft.com/downloads/details.aspx?FamilyID=0fbfde9c-4e76-45e9-b083-e25ccbed9dfd

http://download.microsoft.com/downl...5-99f9-33ac4346a21b/BitmapCopySampleSetup.exe

In Form1() add the following lines :

bmList[3] = MyBitmap.Copy(bmList[0], new Rectangle(25, 25, 50, 50));
bmList[4] = MyBitmap.Copy(bmList[0], new Rectangle(25, 25, 50, 50));

Change the definitions of bmList and nameList to this :

Bitmap[] bmList = new Bitmap[5];
String[] nameList = {"Source", "Clone", "Section","Zoom","Double Zoom"};

Replace the OnPaint() with this :

protected override void OnPaint(PaintEventArgs e)
{
int x = 10;
int y = 0;
int bmIndex = 0;
// Draw each bitmap and label starting at x,y
foreach (Bitmap bm in bmList)
{
// Make sure we are not drawing out of bounds
if (y + 20 + bm.Height > this.Bounds.Bottom)
{
y = 0;
x += 120;
}
// Draw the label
e.Graphics.DrawString(nameList[bmIndex++], font, blackBrush, x, y);
y += 20;
// Draw the bitmap
if ((bm == bmList[3]) || (bm == bmList[4]))
{
Rectangle destPosition;
if (bm == bmList[4])
destPosition = new Rectangle(x,y,bm.Width*2,bm.Height*2);
else
destPosition = new Rectangle(x,y,bm.Width,bm.Height);
int i_Zoom = 4;
int i_OneFourthOfWidth = (int)(bm.Width/i_Zoom);
int i_OneFourthOfHeight = (int)(bm.Height/i_Zoom);
int i_TwoFourthOfWidth = (int)(bm.Width/i_Zoom)*(int)(i_Zoom/2);
int i_TwoFourthOfHeight = (int)(bm.Height/i_Zoom)*(int)(i_Zoom/2);
Rectangle destPortionToShow = new
Rectangle(i_OneFourthOfWidth,i_OneFourthOfHeight,i_TwoFourthOfWidth,i_TwoFou
rthOfHeight);

e.Graphics.DrawImage(bm,destPosition,destPortionToShow,GraphicsUnit.Pixel);
}
else
e.Graphics.DrawImage(bm, x, y);
y += bm.Height + 3;
}
}
Run the program and look at the results.
Maybe this will help as a starter.
Mark Johnson, Berlin Germany
(e-mail address removed)
 
Hi Mark,

Thanks for yr quick response. i'll go through yr example next week. However,
one thing to mention is that Im loading a map (i.e. an image of a .jpg
format or any other format ). would i still be able to use yr code in order
to zoom in and out the image i got on my pocket pc. .

I really appreciate yr help..
Kam
 
The Sample creates a new Bitmap and Paint sit.
Just replace this with the reading in a a file to a Bitmap:
I use this code on the Compact Version (see Save Bitmap Sample)

s_Input =
String.Format(s_FormatBack,Enum.GetName(typeof(Back),i_Standard_Background))
;
Stream s1 = File.OpenRead(s_Input);
bmp_Work[0] = new Bitmap(s1);
s1.Close();
and

Image newImage = Image.FromFile(s_Output_PC); // Read in the .BMP
File
s_Output =
String.Format(s_FormatPC,"Background",Enum.GetName(typeof(Back),i));
newImage.Save(s_Output,ImageFormat.Jpeg); // Save as .Jpeg

If you build a nice Zoom Method, I would be interested to look (and Test)
it.
I don't need it at the moment, but I am sure it will be needed in the
furture.

Mark Johnson, Berlin Germany
(e-mail address removed)
 
The First version of the mail got lost, so I will do it again.

In the Sample used (Copy Bitmap Sample)a Bitmap is created an Painted on.
A Portion is taken out and placed in a new Bitmap and painted at a bigger
size.
Just read in your .jpeg file before this.
Compact Code (taken from Save Bitmap Sample) used be me :

// Do it his way to work on .FrameworkCompact
s_Input =
String.Format(s_FormatCards,Enum.GetName(typeof(Face),i),Enum.GetName(typeof
(Suit),j));
Stream s = File.OpenRead(s_Input);
bmp_Cards[i,j] = new Bitmap(s);
s.Close();
or
newImage = Image.FromFile(s_Output_WinCe); // Read in the .BMP
File
s_Output =
String.Format(s_FormatWINCE,"Background",Enum.GetName(typeof(Back),i));
newImage.Save(s_Output,ImageFormat.Jpeg); // Save as .Jpeg

I don't need the Zoom at the moment, but a sure it will be needed later.
If you work out a myBitmap.Zoom Method, I would be willing to take a look an
try it out .
Take a look at
http://www.mj10777.de/10777Nolle/index.htm
to see what I have in mind for a interesting use.

Mark Johnson, Berlin Germany
(e-mail address removed)
 
Thanks Mark,,

it was very useful. I've finally managed to zoom in and out. However, i'm
having a problem with the scrollbar in the Pocket PC. Im not quite sure if
they are messed up anyway.. I can't get the exact size to scroll. i've
painted the picture on a panel. and then to get the width or the height of
the scrollbar i have created the following metric:

(WidthOfBitmap * TheScale(1,2,3...etc)) - WidthOfPanel +
TheSizeOfTheScrollBar(e.g.10)

However, I always end up with the wrong size.. It's not a liner so at least
I know how to increase the size of the scroll should be everytime I zoom
in...

any idea?

Kam
 
Kam,
Can you describe more clearly what are you trying to do with the scroll bar
calculation?
WidthOfBitmap * Scale - Width OfPanel + SizeOfScrollBar should be the max
value scrolled.
Thanks
Xin
 
Back
Top