dimanche 25 mai 2014

c# - création d'un "swing-o-meter" rotation d'une image qui se trouve dans un pictureBox - Stack Overflow


so I decided to just jump in with learning to code some more and have managed to get rather stuck since I don't know enough basics I think. I have done some work in C and a little in C++ before if that makes any difference.


I decided I wanted to make pointer that is oriented at some angle based on the input. I have everything working bar getting the arrow to rotate. So far I have an arrow bmp image within a pictureBox and the following code which doesn't show up any errors until I build. I'm not entirely sure what else anyone may need to know in order to help so for now I'll just post my code.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void label1_Click(object sender, EventArgs e)
{

}
private Bitmap MoveImageByDesiredAngle(int original_width, int original_height, float desiredAngle)
{
Bitmap resultPicture = new Bitmap(original_width, original_height);
Graphics g = Graphics.FromImage(resultPicture);
g.TranslateTransform((float)original_width / 2, (float)original_height / 2);
g.RotateTransform(desiredAngle);
g.TranslateTransform(-(float)original_width / 2, -(float)original_height / 2);
g.DrawImage(pictureBox1.Image, new Point(0, 0));
return resultPicture;
}
private void button1_Click(object sender, EventArgs e)
{
float o;
float r;

o = float.Parse(textBox1.Text);
r = float.Parse(textBox2.Text);


float ratio;
float angle;
ratio = o / (o + r);
angle = ratio * 180;

Bitmap resultPicture = MoveImageByDesiredAngle(pictureBox3.Image.Width, pictureBox3.Image.Height, angle);
pictureBox3.Image = resultPicture;
}

}
}


so I decided to just jump in with learning to code some more and have managed to get rather stuck since I don't know enough basics I think. I have done some work in C and a little in C++ before if that makes any difference.


I decided I wanted to make pointer that is oriented at some angle based on the input. I have everything working bar getting the arrow to rotate. So far I have an arrow bmp image within a pictureBox and the following code which doesn't show up any errors until I build. I'm not entirely sure what else anyone may need to know in order to help so for now I'll just post my code.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void label1_Click(object sender, EventArgs e)
{

}
private Bitmap MoveImageByDesiredAngle(int original_width, int original_height, float desiredAngle)
{
Bitmap resultPicture = new Bitmap(original_width, original_height);
Graphics g = Graphics.FromImage(resultPicture);
g.TranslateTransform((float)original_width / 2, (float)original_height / 2);
g.RotateTransform(desiredAngle);
g.TranslateTransform(-(float)original_width / 2, -(float)original_height / 2);
g.DrawImage(pictureBox1.Image, new Point(0, 0));
return resultPicture;
}
private void button1_Click(object sender, EventArgs e)
{
float o;
float r;

o = float.Parse(textBox1.Text);
r = float.Parse(textBox2.Text);


float ratio;
float angle;
ratio = o / (o + r);
angle = ratio * 180;

Bitmap resultPicture = MoveImageByDesiredAngle(pictureBox3.Image.Width, pictureBox3.Image.Height, angle);
pictureBox3.Image = resultPicture;
}

}
}

0 commentaires:

Enregistrer un commentaire