Alors voila, c'est un petit prog qui fait un effet de flou et noir et blanc sur une image png (qui a donc un canal alpha). L'image floue est mise sous l'image normalle, selon une position définie par la souris. De ce fait, on produit une sorte d'effet d'ombrage, et la source lumineuse est la souris. Alors d'accord, je sais, l'effet de flou est pourrave. Le truc, c'est que je rétrécis l'image, puis je l'étire. La perte de qualité produit donc un effet de flou, mais c'est pas super joli non plus. D'ailleurs, si vous avez des idées pour améliorer ca, je suis prenneur.
Source / Exemple :
/*
- Created by SharpDevelop.
- User: Yo
- Date: 01/12/2004
- Time: 18:15
-
- To change this template use Tools | Options | Coding | Edit Standard Headers.
- /
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
using System.Drawing.Imaging;
namespace flou{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : System.Windows.Forms.Form{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TrackBar trackBar1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox pictureBox2;
// Ceci est l'image après traitement, à savoir floutée et mise en noir et blanc.
private Bitmap bmpfloufinal;
public MainForm(){
InitializeComponent();
CreateFlou();
}
[STAThread]
public static void Main(string[] args){
Application.Run(new MainForm());
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.trackBar1 = new System.Windows.Forms.TrackBar();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
this.SuspendLayout();
//
// pictureBox2
//
this.pictureBox2.BackColor = System.Drawing.Color.White;
this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
this.pictureBox2.Location = new System.Drawing.Point(24, 24);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(256, 256);
this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox2.TabIndex = 1;
this.pictureBox2.TabStop = false;
//
// pictureBox1
//
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox1.Location = new System.Drawing.Point(312, 24);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(256, 256);
this.pictureBox1.TabIndex = 3;
this.pictureBox1.TabStop = false;
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PictureBox1MouseMove);
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 462);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(784, 24);
this.statusBar1.TabIndex = 2;
this.statusBar1.Text = "statusBar1";
//
// trackBar1
//
this.trackBar1.BackColor = System.Drawing.SystemColors.Control;
this.trackBar1.Location = new System.Drawing.Point(24, 392);
this.trackBar1.Maximum = 120;
this.trackBar1.Minimum = 1;
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(288, 45);
this.trackBar1.TabIndex = 4;
this.trackBar1.Value = 10;
this.trackBar1.Scroll += new System.EventHandler(this.TrackBar1Scroll);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(24, 368);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 16);
this.label1.TabIndex = 5;
this.label1.Text = "Valeur du flou :";
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(784, 486);
this.Controls.Add(this.label1);
this.Controls.Add(this.trackBar1);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.pictureBox2);
this.Name = "MainForm";
this.Text = "MainForm";
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
this.ResumeLayout(false);
}
#endregion
void PictureBox1MouseMove(object sender, System.Windows.Forms.MouseEventArgs e){
this.statusBar1.Text = "Position de la souris : (" + (e.X + this.pictureBox1.Location.X) + ", " + (e.Y + this.pictureBox1.Location.Y) + ") Valeur du flou = " + this.trackBar1.Value;
UpdateMoves(e.X, e.Y);
}
/// <summary>
/// Cette méthode permet de créer une image floue et en noir et blanc.
/// </summary>
private void CreateFlou(){
// Destruction de l'image actuelle si elle existe (la première fois, elle n'existe pas.
if(bmpfloufinal != null) bmpfloufinal.Dispose();
// Création d'une nouvelle image pour faire une petite image qui sera étirée par la suite.
Image bmpflou2 = new Bitmap(this.pictureBox1.Size.Width, this.pictureBox1.Size.Height);
Graphics canvasflou = Graphics.FromImage(bmpflou2);
// Dessinage de l'image en petit.
canvasflou.DrawImage(this.pictureBox2.Image, new Rectangle(0, 0, this.pictureBox1.Size.Width/this.trackBar1.Value, this.pictureBox1.Size.Height/this.trackBar1.Value));
// Création d'un effet noir et blanc.
ColorMatrix cm = new ColorMatrix(new float[][]{new float[]{0.5f,0.5f,0.5f,0,0},
new float[]{0.5f,0.5f,0.5f,0,0},
new float[]{0.5f,0.5f,0.5f,0,0},
new float[]{0,0,0,1,0,0},
new float[]{0,0,0,0,1,0},
new float[]{0,0,0,0,0,1}});
// Définition des attributs de la nouvelle image avec le ColorMatrix noir et blanc
ImageAttributes ia = new ImageAttributes();
ia.SetColorMatrix(cm);
// Création de l'image floue noir et blanc finale.
bmpfloufinal = new Bitmap(this.pictureBox1.Size.Width, this.pictureBox1.Size.Height);
Graphics canvas = Graphics.FromImage(bmpfloufinal);
// On dessine l'image petite, en noir et blanc. Le fait de dessiner une image petite et de l'étirer créé l'effet de flou.
canvas.DrawImage(bmpflou2,new Rectangle(0, 0, bmpfloufinal.Size.Width*this.trackBar1.Value, bmpfloufinal.Size.Height*this.trackBar1.Value),0,0,bmpfloufinal.Size.Width,bmpfloufinal.Size.Height,GraphicsUnit.Pixel,ia);
ia.Dispose();
}
/// <summary>
/// Cette méthode est appelée lorsqu'un mouvement sur l'image est effectué.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
private void UpdateMoves(int x, int y){
// On calcule la position de l'image floue.
int xoffset = -(x- this.pictureBox1.Size.Width / 2);
int yoffset = -(y- this.pictureBox1.Size.Height / 2);
// Création d'un nouveau graphique
Bitmap bmpfinal = new Bitmap(this.pictureBox2.Size.Width, this.pictureBox2.Size.Height);
Graphics canvas = Graphics.FromImage(bmpfinal);
// Dessin de l'image floue créée par la fonction CreateFlou()
canvas.DrawImage(bmpfloufinal, xoffset, yoffset);
// Dessin de l'image normalle
canvas.DrawImage(this.pictureBox2.Image, new Rectangle(0, 0, this.pictureBox2.Size.Width, this.pictureBox2.Size.Height));
// Et envoi vers le composant picture1
this.pictureBox1.Image = bmpfinal;
canvas.Dispose();
}
void TrackBar1Scroll(object sender, System.EventArgs e){
// Cette méthode est appelée lorsque l'on change la valeur du flou. L'image floue et en noir et blanc est regénérée.
CreateFlou();
}
}
}
Conclusion :
Le logiciel dessine avec l'api windows GDI+, et la chose a été créée au moyen de l'excélent SharpDevelop.
Il est aussi à noter que vous voyez l'ombre sur toute l'image parceque l'image de départ est un png transparent.
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.