There are different ways to rotate an image in C#. The first is probably the easiest one but provides just the basic 90, 180, 240 and 360 degree. Here is the way to do this:
For this example you need the library “System.Drawing” and an image.
The next example shows how to turn an image with an given angle:
private Bitmap rotateImage(Image thisImage, float angle)
var newBitmap = new Bitmap(thisImage.Width, thisImage.Height);
using (Graphics g = Graphics.FromImage(newBitmap))
g.TranslateTransform(thisImage.Width / 2, thisImage.Height / 2);
g.TranslateTransform(-thisImage.Width / 2, -thisImage.Height / 2);
g.DrawImage(thisImage, new Point(0, 0));
At the end of the function the grafic draw a new image. Here I used a new point (0/0). This is the orientation of the new draw and left-top of the new picture.
And this is the result:
In .net and c# we have different opportunities to set something transparent. I want use a picture and add a blue semi-transparency. For this example I used a windows-forms-application and added a track bar and a picture box. In my code I use some functions of the library “System.Drawing.Drawing2D”. In the constructor of my main form I add the method “drawBlueTransparency” to the paint-event of the picture box.
trackBar1.Maximum = 255;
pictureBox1.Paint += new PaintEventHandler(drawBlueTransparency);
I also set the maximum of the track bar to 255 because this is the maximum of the alpha value that I used in the event-function (see variable “alphaValue”). As well you could use a max of 10 and recalc the alphaValue.
private void drawBlueTransparency(object sender, PaintEventArgs e)
var rect = new Rectangle(20, 20, 184, 184);
var brush = new SolidBrush(Color.FromArgb(alphaValue, 0, 0, 255));
The method just creates a new rectangle and use it to draw a ellips in it. For this it also needs a brush that is initialized by a color with the alphaValue. If the alphaValue is 0 you will see nothing of the blue ellipse and if you set the alphaValue to 255 (maximum) you just will see the blue ellipse without any transparency. Everything in between is like semi-transparent.
Now I can set different alpha values and use the method “.Refresh()” to redraw the picture box.
private void trackBar1_Scroll(object sender, System.EventArgs e)
alphaValue = trackBar1.Value;
Here you can see the result: