Rotate images .net compact framework

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

Is there anyway to rotate an image in .net compact framework ? Is there another library to do it?

Thanks for all
 
This will work, but is very slow, so use sparenly.

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</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
 
Thanks mar

The problem is that I need to use it a lot of times.... Is there another possible solution?

Thanks
 
So long as you use Get/SetPixel: I am afraid not.
There was a long discussion about this last year and nothing more came out.
SP2 made no changes in the speed.
 
..NET CF does support degenerative rectangles for image blitting to acheive image rotation.

Try this to rotate 180 degrees:

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{

Image i = this.pictureBox1.Image;
//180 degrees
Rectangle destRec = new Rectangle(i.Width,
i.Height,
-i.Width,
-i.Height);
//Flip Image
//Rectangle destRec = new Rectangle(i.Width,0,-i.Width,i.Height);
Rectangle srcRect = new Rectangle(0,0,i.Width,i.Height);
e.Graphics.DrawImage(i,destRec,srcRect,GraphicsUnit.Pixel);
}


HTH...Mark

----- fran wrote: -----

Hi!

Is there anyway to rotate an image in .net compact framework ? Is there another library to do it??

Thanks for all
 
Hi

Thanks for your answe

I knew this possibility, but i don't know how to ratate it 90º degrees that is the rotation I need, I have been trying it but i can't get it

Can you help me?

Thank
 
Hi fran,

Unfortunately, this would require a pixel by pixel redraw. Mark's solution
can still be applied but I don't think it will be any faster than the
Get/SetPixel methods. Here is the code if you want to try it:

int x = 0;
int y = 0;

int dstX = x + bmp.Height - 1;
int dstY = y;
Rectangle src = new Rectangle(0, 0, 1, 1);
for (int r = 0; r < bmp.Height; r++)
{
for (int c = 0; c < bmp.Width; c++)
{
e.Graphics.DrawImage(bmp, dstX, dstY, src, GraphicsUnit.Pixel);
src.X++;
dstY++;
}
dstY = y;
dstX--;
src.X = 0;
src.Y++;
}

If this is something you want to do a lot then you may want to create a new
bitmap and render the rotation to it once at load time, then use that bitmap
every time you require a rotated bitmap. Otherwise, there may be some GDI
functions you can P/Invoke.

You can create your own bitmap objects and render them yourself with GAPI if
you want to make a bigger investment in the project:

Dancing Zombies: Adding Bitmaps to the Managed Graphics Library
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/WrapGAPI2.asp

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.

fran said:
Hi!

Thanks for your answer

I knew this possibility, but i don't know how to ratate it 90º degrees
that is the rotation I need, I have been trying it but i can't get it.
 
Back
Top