473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Im trying to rotate an image but it dosent work good enough:

Im using pictureBox1 and pictureBox2 and a button1
Im doing image cropped and then i can drag the image around the form with the pictureBox2.

Now i did that when i click on the button1 in the button1_Click event it will rotate the image by 25 degrees.

The problem is i want to rotate the original cropped Image and not a new one so this is what i did in the rotation function:

Expand|Select|Wrap|Line Numbers
  1. private  Bitmap RotateImageByAngle(Bitmap oldBitmap, float angle)
  2.         {
  3.  
  4.            // var newBitmap = new Bitmap(oldBitmap.Width, oldBitmap.Height);
  5.             var graphics = Graphics.FromImage(oldBitmap);
  6.             graphics.TranslateTransform((float)oldBitmap.Width / 2, (float)oldBitmap.Height / 2);
  7.             graphics.RotateTransform(angle);
  8.             graphics.TranslateTransform(-(float)oldBitmap.Width / 2, -(float)oldBitmap.Height / 2);
  9.             graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  10.             graphics.DrawImage(oldBitmap,0,0);
  11.             graphics.ResetTransform();
  12.             return oldBitmap;
  13.         }
  14.  
If im using the newBitmap its working but then the new rotated image lose quality and also its rotating inside the pictureBox2 but there is white spaces between the image borders and the pictureBox2 from inside.

So to avoid quality lost i want to rotate somehow the original image ( oldBitmap ).
Now as im doing it the oldBitmap rotating but its keep drawing on it self over and over again.

This is the rotation function when using the newBitmap:

Expand|Select|Wrap|Line Numbers
  1.  private  Bitmap RotateImageByAngle(Bitmap oldBitmap, float angle)
  2.         {
  3.  
  4.             var newBitmap = new Bitmap(oldBitmap.Width, oldBitmap.Height);
  5.             var graphics = Graphics.FromImage(newBitmap);
  6.             graphics.TranslateTransform((float)oldBitmap.Width / 2, (float)oldBitmap.Height / 2);
  7.             graphics.RotateTransform(angle);
  8.             graphics.TranslateTransform(-(float)oldBitmap.Width / 2, -(float)oldBitmap.Height / 2);
  9.             graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  10.             graphics.DrawImage(oldBitmap,0,0);
  11.             graphics.ResetTransform();
  12.             return newBitmap;
  13.         }
  14.  

And this is the complete code including the cropped image.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Drawing2D;
  10. using System.Drawing.Imaging;
  11.  
  12.  
  13. namespace WindowsFormsApplication2
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         Random _rnd = new Random();
  18.         int g = 0;
  19.         int _x = 0;
  20.         int _y = 0;
  21.         int _x2 = 0;
  22.         int _y2 = 0;
  23.         Bitmap _croppedImage = null;
  24.         private Point _curLoc = new Point(0, 0);
  25.  
  26.  
  27.         public Form1()
  28.         {
  29.             InitializeComponent();
  30.             pictureBox1.Image = new Bitmap(@"d:\2011-05-18 08.34.32.jpg");
  31.             pictureBox2.Image = new Bitmap(@"d:\2011-05-18 08.34.32.jpg");
  32.         }
  33.  
  34.         private void Form1_Load(object sender, EventArgs e)
  35.         {
  36.            // GenerateBitmap();
  37.  
  38.         }
  39.  
  40.         private void button1_Click(object sender, EventArgs e)
  41.         {
  42.            // GenerateBitmap();
  43.             g = g + 25;
  44.            pictureBox2.Image =  RotateImageByAngle(_croppedImage, g);
  45.         }
  46.  
  47.         private void GenerateBitmap()
  48.         {
  49.             Bitmap bmp = new Bitmap(this.pictureBox1.ClientSize.Width, this.pictureBox1.ClientSize.Height);
  50.  
  51.             using (Graphics g = Graphics.FromImage(bmp))
  52.             {
  53.                 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  54.                 g.Clear(Color.Green);
  55.  
  56.                 for (int i = 0; i < 10; i++)
  57.                 {
  58.                     Rectangle r = new Rectangle(_rnd.Next(this.pictureBox1.ClientSize.Width),
  59.                       _rnd.Next(this.pictureBox1.ClientSize.Height),
  60.                       _rnd.Next(this.pictureBox1.ClientSize.Width / 2),
  61.                       _rnd.Next(this.pictureBox1.ClientSize.Height / 2));
  62.  
  63.                     if ((i & 0x01) == 1)
  64.                     {
  65.                         g.FillEllipse(Brushes.Aqua, r);
  66.                     }
  67.                     else
  68.                     {
  69.                         g.FillRectangle(Brushes.DarkRed, r);
  70.                     }
  71.                 }
  72.             }
  73.  
  74.             if (this.pictureBox1.Image != null)
  75.                 this.pictureBox1.Image.Dispose();
  76.  
  77.             this.pictureBox1.Image = bmp;
  78.         }
  79.  
  80.         private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  81.         {
  82.             if (e.Button == System.Windows.Forms.MouseButtons.Left)
  83.             {
  84.                 _x = e.X;
  85.                 _y = e.Y;
  86.             }
  87.             else if (e.Button == System.Windows.Forms.MouseButtons.Right)
  88.             {
  89.                 //if we are inside the drawn rectangle
  90.                 if (new Rectangle(e.X, e.Y, 1, 1).IntersectsWith(new Rectangle(_x, _y, _x2 - _x, _y2 - _y)))
  91.                 {
  92.                     //get the image
  93.                     if (_x > -1 && _y > -1 && _x2 < this.pictureBox1.ClientSize.Width && _y2 < this.pictureBox1.ClientSize.Height)
  94.                         CropImage();
  95.  
  96.                     //setup the Control
  97.                     pictureBox2.ClientSize = new Size(_croppedImage.Width, _croppedImage.Height);
  98.                     pictureBox2.BorderStyle = BorderStyle.FixedSingle;
  99.                     pictureBox2.Image = _croppedImage;
  100.                     pictureBox2.BringToFront();
  101.                     //set initial offset
  102.                     _curLoc = new Point(-24, -24);
  103.                     pictureBox2.Location = new Point(e.X, e.Y);
  104.                     //capture the mouse
  105.                     pictureBox2.Capture = true;
  106.                 }
  107.             }
  108.  
  109.         }
  110.  
  111.         private void CropImage()
  112.         {
  113.             if (pictureBox1.Image != null)
  114.             {
  115.                 if (_croppedImage != null)
  116.                     _croppedImage.Dispose();
  117.  
  118.                 _croppedImage = ((Bitmap)this.pictureBox1.Image).Clone(
  119.                   new Rectangle(_x, _y, _x2 - _x, _y2 - _y),
  120.                   this.pictureBox1.Image.PixelFormat);
  121.             }
  122.         }
  123.  
  124.         private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  125.         {
  126.             if (e.Button == System.Windows.Forms.MouseButtons.Left)
  127.             {
  128.                 _x2 = e.X;
  129.                 _y2 = e.Y;
  130.                 this.pictureBox1.Invalidate();
  131.             }
  132.  
  133.         }
  134.  
  135.         private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  136.         {
  137.             //release the mouse from pictureBox2
  138.             this.pictureBox2.Capture = false;
  139.  
  140.         }
  141.  
  142.         private void pictureBox1_Paint(object sender, PaintEventArgs e)
  143.         {
  144.             if (_x > -1 && _y > -1 && _x2 < this.pictureBox1.ClientSize.Width && _y2 < this.pictureBox1.ClientSize.Height)
  145.             {
  146.                 e.Graphics.DrawRectangle(Pens.Blue, new Rectangle(_x, _y, _x2 - _x, _y2 - _y));
  147.             }
  148.  
  149.         }
  150.  
  151.         private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
  152.         {
  153.             //set initial values for re-dragging
  154.             if (e.Button == System.Windows.Forms.MouseButtons.Right)
  155.                 _curLoc = new Point(-e.X, -e.Y);
  156.  
  157.         }
  158.  
  159.         private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
  160.         {
  161.             if (e.Button == System.Windows.Forms.MouseButtons.Right)
  162.             {
  163.                 //move the Control
  164.                 Point mousePos = this.pictureBox1.PointToClient(Control.MousePosition);
  165.                 mousePos.Offset(_curLoc.X, _curLoc.Y);
  166.                 ((PictureBox)sender).Location = mousePos;
  167.             }
  168.  
  169.         }
  170.  
  171.         private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
  172.         {
  173.             //release the mouse from pictureBox2
  174.             this.pictureBox2.Capture = false;
  175.         }
  176.  
  177.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  178.         {
  179.             if (_croppedImage != null)
  180.                 _croppedImage.Dispose();
  181.  
  182.             if (pictureBox1.Image != null)
  183.                 pictureBox1.Image.Dispose();
  184.  
  185.         }
  186.  
  187.  
  188.  
  189.         private  Bitmap RotateImageByAngle(Bitmap oldBitmap, float angle)
  190.         {
  191.  
  192.             var newBitmap = new Bitmap(oldBitmap.Width, oldBitmap.Height);
  193.             var graphics = Graphics.FromImage(newBitmap);
  194.             graphics.TranslateTransform((float)oldBitmap.Width / 2, (float)oldBitmap.Height / 2);
  195.             graphics.RotateTransform(angle);
  196.             graphics.TranslateTransform(-(float)oldBitmap.Width / 2, -(float)oldBitmap.Height / 2);
  197.             graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  198.             graphics.DrawImage(oldBitmap,0,0);
  199.             graphics.ResetTransform();
  200.             return newBitmap;
  201.         }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.     }
  208. }
  209.  

Please if you can show me how to do it according to this code i did.

Thanks a lot.
Jun 7 '11 #1
1 2480
GaryTexmo
1,501 Expert 1GB
Rotations in C# are confusing me. I learned matrix transforms in OpenGL and I feel like these are different. I tried experimenting with it but I can't figure it out... normally rotation works from the image origin so the general idea is to translate it so that the image center is the origin, rotate it, then translate it back, but the translations are... funny. I'd need to spend more time on this to understand it.

That said, google has come to the rescue! :D I found the article you were obviously referencing, but I also found another...

http://social.msdn.microsoft.com/For...-6e7907fd5e16/

I tried this one and had a lot more success. The other benefit is that it rotates matrix and uses that to get the bounds of a rectangle, so your rectangle will always contain your rotated image. Kinda nice :)
Jun 8 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: JavaJunkie | last post by:
When I got the following posting on Monster.com I almost puked. See http://jobsearch.monster.com/getjob.asp?JobID=20663147 They want candidates from IIT (Indian Institute of Technology) / REC...
12
by: GraphicsMark | last post by:
Opinions needed - is VB.net good enough to create a proper vector drawing program with some bitmap processing, i.e. does it have a rich enough feature set, and do the compiled programs run fast...
7
by: Showjumper | last post by:
Hi, I have developed an upload server controls to be reused over a number of projects. One of the tasks it needs to handle is to rotate an image. I want to accoplish this by checking the checkbox...
3
by: byrd48 | last post by:
Hi, I am developing a web site which allows users to upload and share photos. I have a datalist which lists the photos and has the usual edit, update commands. Within the edititemtemplate, I...
3
by: Eduard Witteveen | last post by:
Hello list, I have code the draw MyDrawingObject information on a System.Drawing.Graphics object. The code is more/less the following: I now want to rotate / mirror the object i draw. I've...
8
by: Zytan | last post by:
In VB, you use ControlChars.CrLf (or better, ControlChars.NewLine). In C/C++, we are used to using merely "\n" (or at least I am). It appears "\n" is good enough for RichTextBoxes. Does it...
13
by: newlearner | last post by:
hi, Im new into Ajax and Javascript, I have a doubt... I have a External javascript. and a couple of html files and I load These through ajax request and response. The html pages contain a...
6
by: swethak | last post by:
Hi, I displayed the image taken from database.How to raotate that image using javascript.plz tell that how to start the logic.plz tell that some reference websites.
1
by: Nilla2010 | last post by:
Hi, I would like to rotate image on mouse event.If i move the image left side by mouse it should rotate on left side and vice versa. Thanks in advance
3
by: Chocolade | last post by:
The file logoArea is 512x512 Now all the int variables are the area of the ogi wich is on the right bottom corner. newimage is the image where i want to put the logo inside so in the end when...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.