IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Probleme Image en C#


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Nouveau candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2013
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Madagascar

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2013
    Messages : 1
    Par défaut Probleme Image en C#
    Bonjour tous le monde , j'ai un petit problème : J'ai un programme en C# qui crypte un message dans une image. Pour le faire , j'ai changé le code couleur des premiers pixels de l'image en celui des codes ascii du textes. Le probleme est que j'arrive pas à récuperer par la suite le texte que j'ai crypté dans l'image ! cela me retourne rien du tout !! comme si les code couleur qui j'ai modifié précédemment nous pas été prise en compte or on peut voir sur l'image qu'elle l'ont été : voici le code si vous pouvez m'aider !! merci d'avance

    Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    public void crypter(){
    string text = textBox1.Text;
                int length = text.Length;
                Bitmap gray = new Bitmap(bmp.Width,bmp.Height, PixelFormat.Format16bppRgb555);
                Color pixel;
                for (int i = 0; i < bmp.Width; i++)
                {
                    for (int j = 0; j < bmp.Height; j++)
                    {
                        pixel = bmp.GetPixel(i,j);
                        int R = (int)pixel.R;
                        int G = (int)pixel.G;
                        int B = (int)pixel.B;
                        gray.SetPixel(i, j, Color.FromArgb(R, G, B));
                    }
                }
                int R1 = length;
                gray.SetPixel(0,0, Color.FromArgb(R1, R1, R1));
     
                int count = 0;
                for (int i = 0; i < length; i++)
                {
                    R1 = (int)(text[i]);
                    textBox1.Text += "" + R1 + " ";
                    gray.SetPixel(count, i+1, Color.FromArgb(0,R1, R1, R1));
                    MessageBox.Show("" + gray.GetPixel(count, i+1));
                    count++;
                }
                gray.Save("a_crypt.png");
                MessageBox.Show("Cryptage Effectué");
    }

    et voici le code de décryptage

    Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    public void Decrypt(){ 
    Color pixel;
                int count=0;
     
                bmp = new Bitmap("a_crypt.png");
                pixel = bmp.GetPixel(0,0);
                int R = (int)pixel.R;
                int G = (int)pixel.G;
                int B = (int)pixel.B;
                int A = (int)pixel.A;
                string txt = "";
                for (int i = 0; i < 20; i++)
                {
                    pixel = bmp.GetPixel(count,i+1);
                    MessageBox.Show("" + pixel);
                    R = (int)pixel.R;
                    G = (int)pixel.G;
                    B = (int)pixel.B;
                    A = (int)pixel.A;
                    txt+=(char)(R);
                    count++;
                }
                textBox1.Text = txt;
                MessageBox.Show("Decryptage Fini");
    }

  2. #2
    Membre éclairé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juin 2005
    Messages
    700
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Juin 2005
    Messages : 700
    Par défaut
    il me semble de mémoire (à vérifier) qu'il y a une Methode BitLock à appeller avant de modifier les pixels.

    Je ne suis pas sur de son nom exact ...Lock quelque chose ...

    Bonne chance

  3. #3
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    bonjour

    Ton premier probleme :evite un format compresse,et utilise un format bmp...sinon lors de la sauvegarde les bytes sont compresses...avec perte d'information...
    Ton deuxieme probleme vient du format 16bbp555,qui est un format "packed" qui code un pixel sur 16 bits (chaque composante r,g,b sur 5 bits) ce qui fait que ton caractere R1(32 bits) est caste d'abord par SetPixel() en interne en 16 bits...Ensuite seul les 5 bits de poids forts sont pris en consideration....
    (Idem pour G & B)...
    Le controle entier sur la valeur interne du byte pixel ,ne peut se faire que par Bitmap.Lock et BitmapData....

    Voici le code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Threading;
    using System.Runtime.InteropServices;
     
    namespace WinCryptImage
    {
        public partial class frmBitmapData : Form
        {
            private string appPath = Directory.GetCurrentDirectory() + "/Resources/";
            public frmBitmapData()
            {
                InitializeComponent();
                this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                this.pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                Bitmap bmp = (Bitmap)Image.FromFile(appPath + "Nenuphars.jpg");
                this.pictureBox1.Image = bmp;
                Bitmap gray = ConvertTo16bpp(bmp);
                MessageBox.Show("conversion format :" + gray.PixelFormat.ToString().ToString());
     
              Crypter(gray);
     
            }
     
            private void button2_Click(object sender, EventArgs e)
            {
                Decrypter();
            }
     
     
            public void Crypter(Bitmap bmp)
            {
                string txt = textBox1.Text; 
                int length = txt.Length;
     
                //---------------utilise BitmapData
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                BitmapData data = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat );
                IntPtr ptrPixel = data.Scan0;
                Byte[] bytes = new byte[data.Stride * data.Height];
     
                // Copy  RGB to array.
                Marshal.Copy(ptrPixel, bytes, 0, bytes.Length);
     
                //assigne 1er octet 
                byte R1 =(byte) length;
                bytes[0] = R1;
     
                int count = 0;
                for (int i = 0; i < length ; i++)
                {
                    R1 = (byte)txt[i];
     
                    //saut de ligne
                    data.Stride++;
                    bytes[data.Stride  + count] = R1;
     
                    count++;
                }
     
                // Copy  back to the bitmap
                Marshal.Copy(bytes, 0, ptrPixel, bytes.Length);
     
                //save format bmp  non compresse 
                bmp.UnlockBits(data);
                bmp.Save(appPath + "crypt.bmp", ImageFormat.Bmp);
                MessageBox.Show("Cryptage Effectué");
                this.pictureBox2.Image = bmp;
     
     
            }
     
     
            public void Decrypter()
            {
                Bitmap bmp = (Bitmap)Image.FromFile(appPath + "crypt.bmp");
                MessageBox.Show("format :" + bmp.PixelFormat.ToString().ToString());
     
                //---------------utilise BitmapData
                Rectangle rect = new Rectangle(0, 0, bmp.Width  , bmp.Height) ;
                BitmapData data = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat );
                IntPtr ptrPixel = data.Scan0;
                Byte[] bytes = new byte[data.Stride * data.Height];
                // Copy  RGB to array.
                Marshal.Copy(ptrPixel, bytes, 0, bytes.Length);
     
     
                //lit nbre cars
                int length = bytes[0];
                string txt = string.Empty;
                txt += "nb cars :" + length.ToString() + Environment.NewLine;
                int count = 0;
                byte R1 ;
                for (int i = 0; i <length; i++)
                {
                    //saut de ligne
                    data.Stride++;
                    R1 = bytes[data.Stride + count];
                    txt += " " + (Char)R1;
                    count++;
                }
     
                textBox2.Text = txt;
                bmp.UnlockBits(data);
                MessageBox.Show("Decryptage Fini");
     
            }
     
            //Utilitaire conversion  format Format16bppRgb555
            public Bitmap ConvertTo16bpp(Image img)
            {
                //cree un bitmap format Format16bppRgb555
     
                var bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format16bppRgb555);
     
                // dessine original dessus
                using (var gr = Graphics.FromImage(bmp))
                    gr.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
                return bmp;
            }
        }
    }
    bon code....

  4. #4
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Par défaut
    Citation Envoyé par MABROUKI Voir le message
    Ton premier probleme :evite un format compresse,et utilise un format bmp...sinon lors de la sauvegarde les bytes sont compresses...avec perte d'information...
    Je ne pense pas que ce soit ça le problème : il utilise le format PNG, qui fait une compression sans perte

  5. #5
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    bonjour tomlev

    La compression png et jpeg echoue malgre que l'utilisation de l'api bitmap .lock et bitmadata nous permet de manipuler les bytes pixels directement
    Par ailleurs Bitmap.Set(int alpha ,int r,int g,int b ), signale que ces 4 int sont limites à des bytes) ce qui suggere que cet fonction est approprie aux format d'image 3 bytes par pixel et 4 bytes par pixel...
    On ignore en interne son comportement face à des formats compresses....et des autres formats 16bpppRGB555(5 bit par pixel) ,16bpppRGB565(6bits par pixel)...
    Les formats indexes ne supportent pas du tout setpixel....

    L'api directshow mentionne ce qui suit pour acceder ou setter les 3 bytes pixels "packed" sur 16 bit (word c++ =>uint16 c#) avec toute une cuisine pratiquement impossible en c#....

    sur ce lien msdn:
    http://www.google.fr/url?q=http://ms...6UD_sobaRBKr0Q


    bonne soiree....

  6. #6
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    rebonjour njaka_mada,tomlev...

    Oups je me retracte....sur mon premier code ,ton observation m'ayant longuement reflechir...
    Effectivement png (qui derive lui meme de tiff sous brevet de pixar ) fait de la compression ...

    Mais comment remettre ces bytes pixel dans le formatpixel d'origine ou sont cryptes les caracteres unicodes de njaka_mada !!!
    Mais BitmapData vient à la rescouse ,car il permet de changer une zone rectangulaire de notre bmp crypte au format PNG pour la remettre au format 16bpp555 d'origine....
    Seul inonvenient de cette methode il faut connaitre le "rect" de la zone à verrouiller....
    Moyennant cela ,voici le code (changement uniquement au niveau de decrypt):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
     
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Windows.Forms;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Threading;
    using System.Runtime.InteropServices;
     
    namespace WinCryptImage
    {
        public partial class frmBitmapData : Form
        {
            private string appPath = Directory.GetCurrentDirectory() + "/Resources/";
            public frmBitmapData()
            {
                InitializeComponent();
                this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                this.pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                Bitmap bmp = (Bitmap)Image.FromFile(appPath + "Nenuphars.jpg");
                this.pictureBox1.Image = bmp;
                Bitmap gray = ConvertTo16bpp(bmp);
                MessageBox.Show("conversion format :" + gray.PixelFormat.ToString().ToString());
     
              Crypter(gray);
     
            }
     
            private void button2_Click(object sender, EventArgs e)
            {
                Decrypter(textBox1.Text.Length );
            }
     
     
            public void Crypter(Bitmap bmp)
            {
                string txt = textBox1.Text; 
                int length = txt.Length;
     
                //---------------utilise BitmapData
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                BitmapData data = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat );
                IntPtr ptrPixel = data.Scan0;
                Byte[] bytes = new byte[data.Stride * data.Height];
     
                // Copy  RGB to array.
                Marshal.Copy(ptrPixel, bytes, 0, bytes.Length);
     
                //assigne 1er octet 
                byte R1 =(byte) length;
                bytes[0] = R1;
     
                int count = 0;
                for (int i = 0; i < length ; i++)
                {
                    R1 = (byte)txt[i];
     
                    //saut de ligne
                    data.Stride++;
                    bytes[data.Stride  + count] = R1;
     
                    count++;
                }
     
                // Copy  back to the bitmap
                Marshal.Copy(bytes, 0, ptrPixel, bytes.Length);
     
                //save format bmp  non compresse 
                bmp.UnlockBits(data);
                bmp.Save(appPath + "crypt.png", ImageFormat.Png );
                MessageBox.Show("Cryptage Effectué");
                this.pictureBox2.Image = bmp;
     
            }
     
            //SIGNATURE EXIGE LE PARAM lenText
            public void Decrypter(int lenTxt)
            {
                Bitmap bmp = (Bitmap)Image.FromFile(appPath + "crypt.png");
                MessageBox.Show("format :" + bmp.PixelFormat.ToString().ToString());
     
                //---------------utilise BitmapData
                //---------------changement :le rectangle de la zone à lire
                //---------------pixelformat de la zone:Format16bppRgb555
     
                Rectangle rect = new Rectangle(0, 0, lenTxt, lenTxt);
                BitmapData data = bmp.LockBits(rect,
                    ImageLockMode.ReadWrite,
                    PixelFormat.Format16bppRgb555);
     
                IntPtr ptrPixel = data.Scan0;
                Byte[] bytes = new byte[data.Stride * data.Height];
     
                // Copy  RGB to array.
                Marshal.Copy(ptrPixel, bytes, 0, bytes.Length);
     
     
                //lit nbre cars
                byte R1 = bytes[0];
                int length = R1 ;
     
                string txt = string.Empty;
                txt += "nb cars :" + length.ToString() + Environment.NewLine;
                int count = 0;
     
                for (int i = 0; i <length; i++)
                {
                    //saut de ligne
                    data.Stride++;
                    R1 = bytes[data.Stride + count];
     
                    txt += " " + (Char)R1; 
                    count++;
                }
     
                textBox2.Text = txt;
     
                bmp.UnlockBits(data);
     
                MessageBox.Show("Decryptage Fini");
     
            }
     
            //Utilitaire conversion  format Format16bppRgb555
            public Bitmap ConvertTo16bpp(Image img)
            {
                //cree un bitmap format Format16bppRgb555
     
                var bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format16bppRgb555);
     
                // dessine original dessus
                using (var gr = Graphics.FromImage(bmp))
                    gr.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
                return bmp;
            }
            private static void EncodeAsPNG(Bitmap bmp,string path)
            {
                ImageCodecInfo myImageCodecInfo;
                Encoder myEncoder;
                EncoderParameter myEncoderParameter;
                EncoderParameters myEncoderParameters;
     
                // ImageCodecInfo  that represents  PNG codec.
                myImageCodecInfo = GetEncoderInfo("image/png");
     
                // Encoder object based on the GUID  for the ColorDepth 
                myEncoder = Encoder.ColorDepth;
     
                // array EncoderParameters object.
                // only one EncoderParameter  in the array.
                myEncoderParameters = new EncoderParameters(1);
     
                // Save image with a color depth of 24 bits per pixel.
                myEncoderParameter =
                    new EncoderParameter(myEncoder, 24L);
                myEncoderParameters.Param[0] = myEncoderParameter;
                bmp.Save(path + "Shape.png", myImageCodecInfo, myEncoderParameters);
     
     
     
            }
     
       }
    }
    bon code..............

Discussions similaires

  1. [FLASH MX2004] Problème image qui restent pas fixe
    Par °°° Zen-Spirit °°° dans le forum Flash
    Réponses: 4
    Dernier message: 14/06/2006, 21h16
  2. probleme image en mysql
    Par hiagro dans le forum SQL Procédural
    Réponses: 1
    Dernier message: 10/06/2006, 05h41
  3. [débutant] probleme images
    Par Anthony17 dans le forum Delphi
    Réponses: 7
    Dernier message: 15/05/2006, 16h42
  4. [CSS] Problème image "fixed"
    Par mikedimoi dans le forum Mise en page CSS
    Réponses: 4
    Dernier message: 09/05/2006, 17h30
  5. [ XSL : FO] probleme images identique!!!
    Par chouchou93 dans le forum XSL/XSLT/XPATH
    Réponses: 2
    Dernier message: 08/02/2006, 15h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo