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 :

Problème après la compression d'image c sharp


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Homme Profil pro
    Consultant E-Learning
    Inscrit en
    Mars 2016
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Consultant E-Learning
    Secteur : Santé

    Informations forums :
    Inscription : Mars 2016
    Messages : 10
    Par défaut Problème après la compression d'image c sharp
    Bonjour, je cherche à modifier les pixels de mon image compressé.alors je me suis servis de la méthode Lookbit.
    j'ai trouvé un problème au niveau de la modification des pixels de l'image compressé.
    voici mon code
    pour le codeur Jpeg
    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
     
    private ImageCodecInfo GetEncoderInfo(String mimeType)
            {
                try
                {
                    int j;
                    ImageCodecInfo[] encoders;
                    encoders = ImageCodecInfo.GetImageEncoders();
                    for (j = 0; j < encoders.Length; ++j)
                    {
                        if (encoders[j].MimeType == mimeType)
                            return encoders[j];
                    }
                    return null;
                }
                catch (Exception) { return null; }
            }
     
            private ImageCodecInfo GetEncoder(ImageFormat format)
          {
     
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
     
        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }
        return null;
    }
    Pour le compresseur
    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
     
     
    private void ApplyChanges(int taux)
            {
     
     
     
     
                ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
     
        // Create an Encoder object based on the GUID
        // for the Quality parameter category.
        System.Drawing.Imaging.Encoder myEncoder =
            System.Drawing.Imaging.Encoder.Quality;
     
        // Create an EncoderParameters object.
        // An EncoderParameters object has an array of EncoderParameter
        // objects. In this case, there is only one
        // EncoderParameter object in the array.
     
     
                try
                { EncoderParameters myEncoderParameters = new EncoderParameters(1);
     
                EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder,(long)taux);
                myEncoderParameters.Param[0] = myEncoderParameter;
               Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                System.Drawing.Imaging.BitmapData bmpData =
                 bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                  bmp.PixelFormat);
     
                // Get the address of the first line.
                IntPtr ptr = bmpData.Scan0;
     
                // Declare an array to hold the bytes of the bitmap.
                // This code is specific to a bitmap with 24 bits per pixels.
                int bytes = bmp.Width * bmp.Height * 3;
                byte[] rgbValues = new byte[bytes];
     
                // Copy the RGB values into the array.
                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
     
                // Set every red value to 255.  
                for (int counter = 0; counter < rgbValues.Length - 2; counter += 3)
                {
     
                    rgbValues[counter] = (byte)(rgbValues[counter] +255);
                    rgbValues[counter + 1] = (byte)(rgbValues[counter + 1] + 255);
                    rgbValues[counter + 2] = (byte)(rgbValues[counter + 2] + 255);
     
                }
                // Copy the RGB values back to the bitmap
                System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
     
                liveBitmap = new MemoryStream();
     
                bmp.Save(liveBitmap, jpgEncoder, myEncoderParameters);
     
     
                }
     
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }
    Merci

  2. #2
    Membre habitué
    Homme Profil pro
    Consultant E-Learning
    Inscrit en
    Mars 2016
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Consultant E-Learning
    Secteur : Santé

    Informations forums :
    Inscription : Mars 2016
    Messages : 10
    Par défaut
    a l'aide les amis orientez moi

  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 code est pipe car il est confus ( separation des taches suivant leur fonctions) ,comporte des omissions ,et des presomptions sur le PixelFormat (revoir l'API IMAGING en profondeur et les differents PixelFormats des images avant de les manipuler)....


    code revu avec un form d'exemple :

    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
     
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Runtime.InteropServices;
     
    namespace WinLockBit
    {
        public partial class Form1 : Form
        {
            //rappel : un Octet (Byte anglais)  contient 8 bits....
     
            private int bitPerPixel; // haut parleur : nbre de Bits par pixel !!!
            private int nbBytes;     // haut parleur : nbre d' Octets par pixel !!!
     
            private Bitmap bmp = null;
            private MemoryStream liveBitmap = null ;
            private int taux = 50;
            public Form1()
            {
                InitializeComponent();
     
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                bmp = WinLockBit.Properties.Resources.Koala;
                this.pictureBox1.Image=bmp;
     
                bitPerPixel = Image.GetPixelFormatSize(bmp.PixelFormat) ;
                this.label1.Text = bitPerPixel.ToString();
     
                nbBytes = bitPerPixel / 8;
                this.label2.Text = nbBytes.ToString();
     
                ApplyChanges(taux);         
     
                this.pictureBox2.Image = bmp;
               }
     
            private Bitmap TraitementImage(Bitmap bmp)
            {  
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                BitmapData bmpData =
                bmp.LockBits(rect, ImageLockMode.ReadWrite,bmp.PixelFormat);
     
                // Get the address of the first line.
                IntPtr ptr = bmpData.Scan0;
     
                // MODIFICATION
                //generic : pour tout format de pixel .De plus utilse le stride ou ligne de numerisation
                int bytes = bmpData.Stride  * bmp.Height * nbBytes ;
                //int bytes = bmp.Width * bmp.Height * 3;
                byte[] rgbValues = new byte[bytes];
     
                // Copy the RGB values into the array.
                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
     
     
                // Set every red value to 255.  
                // MODIFICATION
                // si nbBytes =4 ce code doit revu (step à 4+ index de plus)
                for (int counter = 0; counter < rgbValues.Length - 2; counter += 3)
                {
     
                    rgbValues[counter] = (byte)(rgbValues[counter] | 255);
                    rgbValues[counter + 1] = (byte)(rgbValues[counter + 1] + 255);
                    rgbValues[counter + 2] = (byte)(rgbValues[counter + 2] + 255);
     
                }
                // Copy the RGB values back to the bitmap
                Marshal.Copy(rgbValues, 0, ptr, bytes);
     
                // RAJOUTER
                bmp.UnlockBits(bmpData);
                bmpData=null;
     
                return bmp;
     
     
            }
            private void ApplyChanges(int taux)
            {
                ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
                Encoder myEncoder = Encoder.Quality;
     
                try
                {
                    EncoderParameters myEncoderParameters = new EncoderParameters(1);
     
                    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, (long)taux);
                    myEncoderParameters.Param[0] = myEncoderParameter;
     
                    bmp  = TraitementImage(bmp);
     
     
                    liveBitmap = new MemoryStream();
     
                    bmp.Save(liveBitmap, jpgEncoder, myEncoderParameters);
     
     
                }
     
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }
     
     
            private ImageCodecInfo GetEncoderInfo(String mimeType)
            {
                try
                {
                    int j;
                    ImageCodecInfo[] encoders;
                    encoders = ImageCodecInfo.GetImageEncoders();
                    for (j = 0; j < encoders.Length; ++j)
                    {
                        if (encoders[j].MimeType == mimeType)
                            return encoders[j];
                    }
                    return null;
                }
                catch (Exception) { return null; }
            }
     
            private ImageCodecInfo GetEncoder(ImageFormat format)
            {
     
                ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
     
                foreach (ImageCodecInfo codec in codecs)
                {
                    if (codec.FormatID == format.Guid)
                    {
                        return codec;
                    }
                }
                return null;
            }
        }
    }
    bon code....

Discussions similaires

  1. Réponses: 3
    Dernier message: 20/03/2012, 15h26
  2. l'image original aprés la compression
    Par 1google dans le forum Imagerie
    Réponses: 2
    Dernier message: 10/10/2011, 23h20
  3. [XL-2007] Problème de compression d'image en VB sous Excel 2007
    Par GBAGO dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 12/07/2010, 14h12
  4. [Image][Débutant]Compression d'images JPG en Java ?
    Par joe.satriani dans le forum 2D
    Réponses: 3
    Dernier message: 30/01/2005, 16h34
  5. probléme de cadre sur une image qui me sert de lien
    Par thomas_chamas dans le forum Balisage (X)HTML et validation W3C
    Réponses: 4
    Dernier message: 26/11/2004, 17h36

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