rotate an image in a C# .NET CF app

A

Arjun Venkataram

Hello,

Is there a way for me to programatically rotate an
image in a C# .NET CF application. I am trying to display
an image in landscape mode when a user clicks on a button
to view a larger version of an image.

thank you,

@rjun
 
M

Mark Johnson

You must do so Pixel by Pixel.
Get/Set Pixel is however on Compact SP1 and SP2 very slow!
Here is a Method that was a result of a discusion here a few months ago.
Hope this helps.

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

#region Bitmap_Rotate
/// <summary>
/// <list type="table">
/// <item><description>Rotate Bitmap</description></item>
/// </list>
/// The SetPixel, GetPixel calls are extremly slow on CF.
/// <remarks>
/// <para>bmp.Width = 29 ; bmp.Height = 41</para>
/// <para>0 ; i_Mirror=0 : Left,Top(0,0) will become
Left,Top(0,0)</para>
/// <para>0 ; i_Mirror=1 : Left,Top(0,0) will become
Right,Top(29,0)</para>
/// <para>90 ; i_Mirror=0 : Left,Top(0,0) will become
Right,Top(41,0)</para>
/// <para>90 ; i_Mirror=1 : Left,Top(0,0) will become
Right,Bottom(41,29)</para>
/// <para>180 ; i_Mirror=0 : Left,Top(0,0) will become
Right,Bottom(29.41)</para>
/// <para>180 ; i_Mirror=1 : Left,Top(0,0) will become
Right,Bottom(0.41)</para>
/// <para>270 ; i_Mirror=0 : Left,Top(0,0) will become
Bottom,Left(0,29)</para>
/// <para>270 ; i_Mirror=1 : Left,Top(0,0) will become
Bottom,Right(0,29)</para>
/// <para>The SetPixel, GetPixel calls are extremly slow on
Framework.Compact (SP1,SP2)</para>
/// <para>Do not use while Drawing, but load and Convert needed Bitmaps
at Startup</para>
/// <para>TODO : 45 - no Idea how do do this</para>
/// <para>some thoughts : Calculate how many Pixel from (0,0) to
(Width,Height)</para>
/// <para> : Build new Rectangel/Bitmap with new (bigger)
size</para>
/// <para> : Fill Bitmap with transparent Colour</para>
/// <para> :
ImageAttributes.SetColorKey(Color.Magenta,Color.Magenta)</para>
/// <para> : Draw Bitmap accourding to some Formel</para>
/// <para>UNKNOWN : Formel to do this</para>
/// <para>Mark Johnson, Berlin Germany - (e-mail address removed)</para>
/// </remarks>
/// </summary>
/// <param name="i_Degree">Supported ; 0,90,180,270</param>
/// <param name="i_Mirror">0=No ; 1 = Yes</param>
/// <param name="bmp">Bitmap to convert</param>
/// <returns>Converted Bitmap</returns>
/// <example>
/// <code>
/// if (i == i_Work_Back_Rotate)
/// bmp_Work = Bitmap_Rotate(90,0,bmp_Work[i_Work_Back]);
/// </code>
/// </example>
public Bitmap Bitmap_Rotate(int i_Degree, int i_Mirror, Bitmap bmp)
{
Bitmap bmp_New = null;
if ((i_Degree != 0) && (i_Degree != 90) && (i_Degree != 180) && (i_Degree
!= 270))
i_Degree = 180; // Assume 180, otherwise no idea what to do
if ((i_Degree == 90) || (i_Degree == 270))
{
bmp_New = new Bitmap(bmp.Height,bmp.Width);
}
if ((i_Degree == 0) || (i_Degree == 180))
{
bmp_New = new Bitmap(bmp.Width,bmp.Height);
}
for (int i=0;i<bmp.Width;i++) // bmp_Width(29)
{
for (int j=0;j<bmp.Height;j++) // bmp_Height(41)
{
if (i_Degree == 0)
{
if (i_Mirror == 0) // Left,Top(0,0) will become Left,Top(0,0)
bmp_New.SetPixel(i,j,bmp.GetPixel(i,j));
else // Left,Top(0,0) will become Right,Top(29,0)
bmp_New.SetPixel(((bmp_New.Width-1)-i),j,bmp.GetPixel(i,j));
} // if (i_Degree == 0)
if (i_Degree == 90)
{
if (i_Mirror == 0) // Left,Top(0,0) will become Right,Top(41,0)
bmp_New.SetPixel(((bmp_New.Width-1)-j),i,bmp.GetPixel(i,j));
else // Left,Top(0,0) will become Right,Bottom(41,29)

bmp_New.SetPixel(((bmp_New.Width-1)-j),((bmp_New.Height-1)-i),bmp.GetPixel(i
,j));
} // if (i_Degree == 90)
if (i_Degree == 180)
{
if (i_Mirror == 0) // Left,Top(0,0) will become Right,Bottom(29.41)

bmp_New.SetPixel(((bmp_New.Width-1)-i),((bmp_New.Height-1)-j),bmp.GetPixel(i
,j));
else // Left,Top(00) will become Left,Bottom(0,41)
bmp_New.SetPixel(i,((bmp_New.Height-1)-j),bmp.GetPixel(i,j));
} // if (i_Degree == 180) // Left,Top(0,0) will become
Right,Bottom(29.41)
if (i_Degree == 270)
{
if (i_Mirror == 0) // Left,Top(0,0) will become Bottom,Left(0,29)
bmp_New.SetPixel(j,((bmp_New.Height-1)-i),bmp.GetPixel(i,j));
else // Left,Top(0,0) will become Bottom,Right(0,29)
bmp_New.SetPixel(j,i,bmp.GetPixel(i,j));
} // if (i_Degree == 270) // Left,Top(0,0) will become
Bottom,Left(0,29)
} // for (int j=0;j<bmp.Height;j++)
} // for (int i=0;i<bmp.Width;i++)
return bmp_New;
} // public Bitmap Bitmap_Rotate(int i_Degree,Bitmap bmp)
#endregion
 

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

Similar Threads


Top