Picturebox

Résolu
johnnous Messages postés 100 Date d'inscription jeudi 3 mars 2005 Statut Membre Dernière intervention 24 mai 2013 - 12 mars 2010 à 13:07
yohan49 Messages postés 380 Date d'inscription samedi 22 janvier 2005 Statut Membre Dernière intervention 13 août 2011 - 22 mars 2010 à 20:56
bonjour
j'ai un petit soucis
j'ai une picturebox avec une image de font
sur celle ci j'ajoute d'autre picturebox fille
je dois pouvoir deplace et faire tourne mes picturebox en gérant la transparence

mom pb est que si une de mes picturebox fille passe sur une autre la transparence ne fonctionne pas
donc je me suis dit je vais faire une capture d'image CopyFromScreen et recharge ma picturebox avec la nouvelle image de font
mais je dois faire avant de faire ma capture disparaitre le picturebox active donc je la rend pas visible
mais si je fait ma capture à ce moment elle est présente dans la capture, et je ne sais pas quelle evenement me dit que la picturebox n'est plus visible j'ai essayer avec paint ou onvisiblechanged mais cela ne marche pas

quelqu'un aurrais une actuce


voici les sources
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
namespace RotateImage
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.OpenFileDialog ofd;
private Bitmap img = null;
private System.Windows.Forms.Button loadImage;
private System.Windows.Forms.NumericUpDown angle;
private System.Windows.Forms.PictureBox pictureBox;
private PictureBox MyPictureBox;
private Button bCopyImg;
private Button button1;
private PictureBox pictureBox1;
private NumericUpDown numericUpDown1;
private PictureBox PicConv;
private PictureBox pictureBox2;
private Button button2;
private NumericUpDown numericUpDown2;
private PictureBox pictureBox3;
private NumericUpDown numericUpDown3;
private PictureBox pictureBox4;
private Button button3;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.loadImage = new System.Windows.Forms.Button();
this.angle = new System.Windows.Forms.NumericUpDown();
this.label1 = new System.Windows.Forms.Label();
this.ofd = new System.Windows.Forms.OpenFileDialog();
this.bCopyImg = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.PicConv = new System.Windows.Forms.PictureBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.MyPictureBox = new System.Windows.Forms.PictureBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button2 = new System.Windows.Forms.Button();
this.numericUpDown2 = new System.Windows.Forms.NumericUpDown();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.numericUpDown3 = new System.Windows.Forms.NumericUpDown();
this.pictureBox4 = new System.Windows.Forms.PictureBox();
this.button3 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.angle)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.PicConv)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.MyPictureBox)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown3)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).BeginInit();
this.SuspendLayout();
//
// loadImage
//
this.loadImage.Location = new System.Drawing.Point(8, 8);
this.loadImage.Name = "loadImage";
this.loadImage.Size = new System.Drawing.Size(88, 23);
this.loadImage.TabIndex = 0;
this.loadImage.Text = "Load &Image...";
this.loadImage.Click += new System.EventHandler(this.loadImage_Click);
//
// angle
//
this.angle.DecimalPlaces = 1;
this.angle.Enabled = false;
this.angle.Location = new System.Drawing.Point(176, 8);
this.angle.Maximum = new decimal(new int[] {
360,
0,
0,
0});
this.angle.Minimum = new decimal(new int[] {
1,
0,
0,
-2147483648});
this.angle.Name = "angle";
this.angle.Size = new System.Drawing.Size(64, 20);
this.angle.TabIndex = 1;
this.angle.ValueChanged += new System.EventHandler(this.angle_ValueChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(104, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(71, 13);
this.label1.TabIndex = 2;
this.label1.Text = "&Rotate Image";
//
// bCopyImg
//
this.bCopyImg.Location = new System.Drawing.Point(317, 9);
this.bCopyImg.Name = "bCopyImg";
this.bCopyImg.Size = new System.Drawing.Size(130, 21);
this.bCopyImg.TabIndex = 5;
this.bCopyImg.Text = "CopyImg";
this.bCopyImg.UseVisualStyleBackColor = true;
this.bCopyImg.Click += new System.EventHandler(this.bCopyImg_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(458, 59);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(97, 28);
this.button1.TabIndex = 6;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// numericUpDown1
//
this.numericUpDown1.Increment = new decimal(new int[] {
5,
0,
0,
0});
this.numericUpDown1.Location = new System.Drawing.Point(584, 78);
this.numericUpDown1.Maximum = new decimal(new int[] {
1000,
0,
0,
0});
this.numericUpDown1.Minimum = new decimal(new int[] {
1000,
0,
0,
-2147483648});
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.Size = new System.Drawing.Size(90, 20);
this.numericUpDown1.TabIndex = 8;
this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged);
//
// pictureBox2
//
this.pictureBox2.BackColor = System.Drawing.Color.Transparent;
this.pictureBox2.Image = global::RotateImage.Properties.Resources.cadenas1;
this.pictureBox2.Location = new System.Drawing.Point(119, 401);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(65, 52);
this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox2.TabIndex = 10;
this.pictureBox2.TabStop = false;
//
// PicConv
//
this.PicConv.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.PicConv.Location = new System.Drawing.Point(560, 135);
this.PicConv.Name = "PicConv";
this.PicConv.Size = new System.Drawing.Size(313, 97);
this.PicConv.TabIndex = 9;
this.PicConv.TabStop = false;
//
// pictureBox
//
this.pictureBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox.BackColor = System.Drawing.Color.Transparent;
this.pictureBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox.Location = new System.Drawing.Point(60, 181);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(134, 76);
this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.pictureBox.TabIndex = 3;
this.pictureBox.TabStop = false;
this.pictureBox.VisibleChanged += new System.EventHandler(this.pictureBox_VisibleChanged);
this.pictureBox.Click += new System.EventHandler(this.pictureBox_Click);
this.pictureBox.PaddingChanged += new System.EventHandler(this.pictureBox_PaddingChanged);
this.pictureBox.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox_Paint);
//
// MyPictureBox
//
this.MyPictureBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.MyPictureBox.Location = new System.Drawing.Point(242, 60);
this.MyPictureBox.Name = "MyPictureBox";
this.MyPictureBox.Size = new System.Drawing.Size(149, 75);
this.MyPictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.MyPictureBox.TabIndex = 4;
this.MyPictureBox.TabStop = false;
//
// pictureBox1
//
this.pictureBox1.Image = global::RotateImage.Properties.Resources.Palette_dessus;
this.pictureBox1.Location = new System.Drawing.Point(213, 181);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(341, 201);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 7;
this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
this.pictureBox1.Validating += new System.ComponentModel.CancelEventHandler(this.pictureBox1_Validating);
//
// button2
//
this.button2.Location = new System.Drawing.Point(146, 75);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(71, 37);
this.button2.TabIndex = 11;
this.button2.Text = "Parent";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// numericUpDown2
//
this.numericUpDown2.DecimalPlaces = 1;
this.numericUpDown2.Location = new System.Drawing.Point(518, 12);
this.numericUpDown2.Maximum = new decimal(new int[] {
360,
0,
0,
0});
this.numericUpDown2.Minimum = new decimal(new int[] {
1,
0,
0,
-2147483648});
this.numericUpDown2.Name = "numericUpDown2";
this.numericUpDown2.Size = new System.Drawing.Size(64, 20);
this.numericUpDown2.TabIndex = 12;
this.numericUpDown2.ValueChanged += new System.EventHandler(this.numericUpDown2_ValueChanged);
//
// pictureBox3
//
this.pictureBox3.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox3.BackColor = System.Drawing.Color.Transparent;
this.pictureBox3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox3.Location = new System.Drawing.Point(60, 282);
this.pictureBox3.Name = "pictureBox3";
this.pictureBox3.Size = new System.Drawing.Size(134, 76);
this.pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.pictureBox3.TabIndex = 13;
this.pictureBox3.TabStop = false;
this.pictureBox3.VisibleChanged += new System.EventHandler(this.pictureBox3_VisibleChanged);
this.pictureBox3.Click += new System.EventHandler(this.pictureBox3_Click);
//
// numericUpDown3
//
this.numericUpDown3.Increment = new decimal(new int[] {
5,
0,
0,
0});
this.numericUpDown3.Location = new System.Drawing.Point(584, 109);
this.numericUpDown3.Maximum = new decimal(new int[] {
1000,
0,
0,
0});
this.numericUpDown3.Minimum = new decimal(new int[] {
1000,
0,
0,
-2147483648});
this.numericUpDown3.Name = "numericUpDown3";
this.numericUpDown3.Size = new System.Drawing.Size(90, 20);
this.numericUpDown3.TabIndex = 14;
this.numericUpDown3.ValueChanged += new System.EventHandler(this.numericUpDown3_ValueChanged);
//
// pictureBox4
//
this.pictureBox4.Location = new System.Drawing.Point(619, 316);
this.pictureBox4.Name = "pictureBox4";
this.pictureBox4.Size = new System.Drawing.Size(236, 137);
this.pictureBox4.TabIndex = 15;
this.pictureBox4.TabStop = false;
//
// button3
//
this.button3.Location = new System.Drawing.Point(458, 102);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(95, 26);
this.button3.TabIndex = 16;
this.button3.Text = "button3";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(938, 525);
this.Controls.Add(this.button3);
this.Controls.Add(this.pictureBox4);
this.Controls.Add(this.numericUpDown3);
this.Controls.Add(this.pictureBox3);
this.Controls.Add(this.numericUpDown2);
this.Controls.Add(this.button2);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.PicConv);
this.Controls.Add(this.numericUpDown1);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.button1);
this.Controls.Add(this.bCopyImg);
this.Controls.Add(this.MyPictureBox);
this.Controls.Add(this.label1);
this.Controls.Add(this.angle);
this.Controls.Add(this.loadImage);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Image Rotation demo";
((System.ComponentModel.ISupportInitialize)(this.angle)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.PicConv)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.MyPictureBox)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown3)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void loadImage_Click(object sender, System.EventArgs e)
{
if( ofd.ShowDialog() == DialogResult.OK )
{
//pictureBox1.Invalidate();

Image image = new Bitmap(ofd.FileName);


//Image imageNew = new Bitmap(200, 200); //
Image image2 = new Bitmap(image, image.Width / 3, image.Height / 3); //la redimensionne
image = new Bitmap(image, image.Width / 2, image.Height / 4); //la redimensionne
Image imageNew = new Bitmap((image2.Width + image.Width) * 2, image.Height * 2);
Graphics g = Graphics.FromImage(imageNew);
// g.TranslateTransform(100, 10);
int PosXImg1 = imageNew.Width / 2 + image.Width - (image.Width + image2.Width) / 2;// -(image2.Width + image.Width) / 2 - image2.Width;// -image.Width;
int PosYImg1 = imageNew.Height / 2 - image2.Height / 2;

g.DrawImage(image2, PosXImg1, PosYImg1);

g.DrawImage(image, PosXImg1 - image.Width, PosYImg1);
g.DrawString("toto", this.Font, Brushes.Black, new PointF(PosXImg1, PosYImg1));
// g.TranslateTransform(-(image.Width + image2.Width)/2, 0);
// g.DrawImage(imageNew , new PointF(0, 0));
g.DrawLine(Pens.Red, new Point(imageNew.Width / 2, PosYImg1 - 40), new Point(imageNew.Width / 2, PosXImg1 + 40));
// g.DrawLine(Pens.Red, new Point(imageNew.Height / 2, 50), new Point(imageNew.Height / 2, 150));

g.Dispose();
// pictureBox.Image = (Image)imageNew.Clone();
MyPictureBox.Image = (Image)imageNew.Clone();// passage par une image Tempo olbigatoire sinon image pas net
// MyPictureBox.ImageLocation = ofd.FileName;
// img = (Bitmap) Bitmap.FromFile(ofd.FileName);
pictureBox.Parent = pictureBox1;
pictureBox3.Parent = pictureBox1;
// pictureBox2.Parent = pictureBox1;
pictureBox2.Left = pictureBox1.Width / 2;
pictureBox2.Top = pictureBox1.Height / 2;
angle.Enabled = true;
pictureBox.Left = 50;
pictureBox.Top = 10;
int SizeMax =0;
if (MyPictureBox.Height > MyPictureBox.Height) SizeMax = MyPictureBox.Height;
else SizeMax = MyPictureBox.Width ;
pictureBox.Height = SizeMax;
pictureBox.Width = SizeMax;
pictureBox3.Height = SizeMax;
pictureBox3.Width = SizeMax;

pictureBox3.Left = 10;
pictureBox3.Top = 10;

pictureBox1.Refresh();
//pictureBox3.Height = pictureBox1.Height;
//pictureBox3.Width = pictureBox1.Width;

// Rotate/draw
// angle_ValueChanged(null, EventArgs.Empty);
}
}

private void angle_ValueChanged(object sender, System.EventArgs e)
{
if( angle.Value > 359.9m )
{
angle.Value = 0;
return ;
}

if( angle.Value < 0.0m )
{
angle.Value = 359;
return ;
}
//img = (Bitmap) MyPictureBox.Image.Clone();
Image oldImage = pictureBox.Image;
pictureBox.Image = Utilities.RotateImage((Bitmap)MyPictureBox.Image.Clone(), (float)angle.Value);
pictureBox.Refresh();
pictureBox.BringToFront();
if( oldImage != null )
{
oldImage.Dispose();
}
}

private void bCopyImg_Click(object sender, EventArgs e)
{
// MyPictureBox.Visible = true;

Bitmap bitmap = new Bitmap(this.MyPictureBox.Width, this.MyPictureBox.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bitmap))
{
Point p = this.PointToScreen(this.MyPictureBox.Location);
g.CopyFromScreen(p.X, p.Y, 0, 0, this.MyPictureBox.Size, CopyPixelOperation.SourceCopy);
g.Dispose();
}

//pictureBox.Height = MyPictureBox.Height;
//pictureBox.Width = MyPictureBox.Width;
pictureBox.Image = bitmap;
// img = (Bitmap)pictureBox.Image;
img = bitmap ;//(Bitmap)MyPictureBox.Image;
MyPictureBox.Visible = false;
}

private void button1_Click(object sender, EventArgs e)
{
pictureBox.Visible = !pictureBox.Visible;
}

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
pictureBox.Left =(int) numericUpDown1.Value;
pictureBox.BringToFront();
}

private void button2_Click(object sender, EventArgs e)
{
pictureBox2.Parent = pictureBox1;
pictureBox2.Left = pictureBox1.Width / 2;
pictureBox2.Top = pictureBox1.Height / 2;
pictureBox2.BringToFront();
}

private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{

if (angle.Value > 359.9m)
{
angle.Value = 0;
return;
}

if (angle.Value < 0.0m)
{
angle.Value = 359;
return;
}
//img = (Bitmap) MyPictureBox.Image.Clone();
Image oldImage = pictureBox3.Image;
pictureBox3.Image = Utilities.RotateImage((Bitmap)MyPictureBox.Image.Clone(), (float)numericUpDown2.Value);
pictureBox3.BringToFront();
if (oldImage != null)
{
oldImage.Dispose();
}
}

private void numericUpDown3_ValueChanged(object sender, EventArgs e)
{
pictureBox.Top = (int) numericUpDown3.Value;
pictureBox.BringToFront();
}

bool bPictureBox = false;
private void pictureBox_Click(object sender, EventArgs e)
{
bPictureBox = true;
//int MposLeft = pictureBox.Left;
//pictureBox.Left = 3000;
//pictureBox.Invalidate();

//pictureBox.Left = MposLeft;
pictureBox.Visible = false ;
}

private void pictureBox3_Click(object sender, EventArgs e)
{

}

private void button3_Click(object sender, EventArgs e)
{
pictureBox.Visible = false;
}
private void Copy()
{
pictureBox.Refresh();
pictureBox.Invalidate();
Bitmap bitmap = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bitmap))
{
Point p = this.PointToScreen(this.pictureBox1.Location);
g.CopyFromScreen(p.X, p.Y, 0, 0, this.pictureBox1.Size, CopyPixelOperation.SourceCopy);
g.Dispose();
}
pictureBox1.Image = bitmap;
pictureBox1.Invalidate();
}

private void pictureBox3_VisibleChanged(object sender, EventArgs e)
{
if (!pictureBox3.Visible && bPictureBox)
{
bPictureBox = false;
Copy();
pictureBox3.Visible = true;
}
pictureBox.Visible = true;

}

private void pictureBox_VisibleChanged(object sender, EventArgs e)
{
// MessageBox.Show("Visble");
//pictureBox.Refresh();
if (!pictureBox.Visible && bPictureBox)
{

// bPictureBox = false;
// Copy();
// pictureBox.Visible = true;
}
//pictureBox.Visible = true;
}

private void pictureBox_PaddingChanged(object sender, EventArgs e)
{
MessageBox.Show("padding");
}

private void pictureBox_Paint(object sender, PaintEventArgs e)
{
//if (bPictureBox )
// MessageBox.Show("paint");
}

private void pictureBox1_Validating(object sender, CancelEventArgs e)
{
MessageBox.Show("Validating");
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (bPictureBox)
{
MessageBox.Show("pictureBox1_Paint" + ((PictureBox)sender).Name );
bPictureBox = false;
}
}
}
}



***************************************
la classe utilities
*****************************************

using System;
using System.Drawing;

namespace RotateImage
{
/// <summary>
/// Summary description for Utilities.
/// </summary>
public sealed class Utilities
{
private Utilities()
{
}

/// <summary>
/// Creates a new Image containing the same image only rotated
/// </summary>
/// The <see cref="System.Drawing.Image"/> to rotate


/// The amount to rotate the image, clockwise, in degrees


/// <returns>A new <see cref="System.Drawing.Bitmap"/> that is just large enough
/// to contain the rotated image without cutting any corners off.</returns>
/// <exception cref="System.ArgumentNullException">Thrown if <see cref="image"/> is null.</exception>
public static Bitmap RotateImage(Image image, float angle)
{
if(image == null)
throw new ArgumentNullException("image");

const double pi2 = Math.PI / 2.0;

// Why can't C# allow these to be const, or at least readonly
// *sigh* I'm starting to talk like Christian Graus :omg:
double oldWidth = (double) image.Width;
double oldHeight = (double) image.Height;

// Convert degrees to radians
double theta = ((double) angle) * Math.PI / 180.0;
double locked_theta = theta;

// Ensure theta is now [0, 2pi)
while( locked_theta < 0.0 )
locked_theta += 2 * Math.PI;

double newWidth, newHeight;
int nWidth, nHeight; // The newWidth/newHeight expressed as ints

#region Explaination of the calculations
/*
* The trig involved in calculating the new width and height
* is fairly simple; the hard part was remembering that when
* PI/2 <= theta <= PI and 3PI/2 <= theta < 2PI the width and
* height are switched.
*
* When you rotate a rectangle, r, the bounding box surrounding r
* contains for right-triangles of empty space. Each of the
* triangles hypotenuse's are a known length, either the width or
* the height of r. Because we know the length of the hypotenuse
* and we have a known angle of rotation, we can use the trig
* function identities to find the length of the other two sides.
*
* sine = opposite/hypotenuse
* cosine = adjacent/hypotenuse
*
* solving for the unknown we get
*
* opposite = sine * hypotenuse
* adjacent = cosine * hypotenuse
*
* Another interesting point about these triangles is that there
* are only two different triangles. The proof for which is easy
* to see, but its been too long since I've written a proof that
* I can't explain it well enough to want to publish it.
*
* Just trust me when I say the triangles formed by the lengths
* width are always the same (for a given theta) and the same
* goes for the height of r.
*
* Rather than associate the opposite/adjacent sides with the
* width and height of the original bitmap, I'll associate them
* based on their position.
*
* adjacent/oppositeTop will refer to the triangles making up the
* upper right and lower left corners
*
* adjacent/oppositeBottom will refer to the triangles making up
* the upper left and lower right corners
*
* The names are based on the right side corners, because thats
* where I did my work on paper (the right side).
*
* Now if you draw this out, you will see that the width of the
* bounding box is calculated by adding together adjacentTop and
* oppositeBottom while the height is calculate by adding
* together adjacentBottom and oppositeTop.
*/
#endregion

double adjacentTop, oppositeTop;
double adjacentBottom, oppositeBottom;

// We need to calculate the sides of the triangles based
// on how much rotation is being done to the bitmap.
// Refer to the first paragraph in the explaination above for
// reasons why.
if( (locked_theta >= 0.0 && locked_theta < pi2) ||
(locked_theta >= Math.PI && locked_theta < (Math.PI + pi2) ) )
{
adjacentTop = Math.Abs(Math.Cos(locked_theta)) * oldWidth;
oppositeTop = Math.Abs(Math.Sin(locked_theta)) * oldWidth;

adjacentBottom = Math.Abs(Math.Cos(locked_theta)) * oldHeight;
oppositeBottom = Math.Abs(Math.Sin(locked_theta)) * oldHeight;
}
else
{
adjacentTop = Math.Abs(Math.Sin(locked_theta)) * oldHeight;
oppositeTop = Math.Abs(Math.Cos(locked_theta)) * oldHeight;

adjacentBottom = Math.Abs(Math.Sin(locked_theta)) * oldWidth;
oppositeBottom = Math.Abs(Math.Cos(locked_theta)) * oldWidth;
}

newWidth = adjacentTop + oppositeBottom;
newHeight = adjacentBottom + oppositeTop;

nWidth = (int) Math.Ceiling(newWidth);
nHeight = (int) Math.Ceiling(newHeight);

Bitmap rotatedBmp = new Bitmap(nWidth, nHeight);
rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using(Graphics g = Graphics.FromImage(rotatedBmp))
{
// This array will be used to pass in the three points that
// make up the rotated image
Point [] points;

/*
* The values of opposite/adjacentTop/Bottom are referring to
* fixed locations instead of in relation to the
* rotating image so I need to change which values are used
* based on the how much the image is rotating.
*
* For each point, one of the coordinates will always be 0,
* nWidth, or nHeight. This because the Bitmap we are drawing on
* is the bounding box for the rotated bitmap. If both of the
* corrdinates for any of the given points wasn't in the set above
* then the bitmap we are drawing on WOULDN'T be the bounding box
* as required.
*/
if( locked_theta >= 0.0 && locked_theta < pi2 )
{
points = new Point[] {
new Point( (int) oppositeBottom, 0 ),
new Point( nWidth, (int) oppositeTop ),
new Point( 0, (int) adjacentBottom )
};

}
else if( locked_theta >= pi2 && locked_theta < Math.PI )
{
points = new Point[] {
new Point( nWidth, (int) oppositeTop ),
new Point( (int) adjacentTop, nHeight ),
new Point( (int) oppositeBottom, 0 )
};
}
else if( locked_theta >= Math.PI && locked_theta < (Math.PI + pi2) )
{
points = new Point[] {
new Point( (int) adjacentTop, nHeight ),
new Point( 0, (int) adjacentBottom ),
new Point( nWidth, (int) oppositeTop )
};
}
else
{
points = new Point[] {
new Point( 0, (int) adjacentBottom ),
new Point( (int) oppositeBottom, 0 ),
new Point( (int) adjacentTop, nHeight )
};
}

g.DrawImage(image, points);
}
int tmp = image.Width;
int tmp2 = image.Height;
return rotatedBmp;
}
}
}


Jean louis

3 réponses

yohan49 Messages postés 380 Date d'inscription samedi 22 janvier 2005 Statut Membre Dernière intervention 13 août 2011 7
22 mars 2010 à 20:56
public class PictureBoxAternative : PictureBox
{
    Bitmap[] AllBitmap = new Bitmap[10]; //A remplir par des Image VAlide

    int indexBitmap = 0;

    protected override OnClick(EventArg e)
    {
       base.OnClick(e);

       if(indexBitmap > AllBitmap.Lenght)
           indexBitmap = 0;

       Image = AllBitmap[indexBitmap++]


    }
}
3
cs_Robert33 Messages postés 834 Date d'inscription samedi 15 novembre 2008 Statut Membre Dernière intervention 14 janvier 2017 33
13 mars 2010 à 16:20
Bonjour

S'il te plais, ne post pas la totalité de ton code, il y a peu de chance que quelqu'un le test !!!

à la place de pictureBox, pourquoi n'essaye tu pas de dessiner manuellement tes images dans la méthode OnPaint ?

C# is amazing, enjoy it!
0
johnnous Messages postés 100 Date d'inscription jeudi 3 mars 2005 Statut Membre Dernière intervention 24 mai 2013
15 mars 2010 à 06:43
bonjour
car j'ai besoin de deplace mes images une fois que je clique dessus


Jean louis
0
Rejoignez-nous