/// <summary> /// Inscrit dans l'image le texte donné en paramètre dans l'image donnée en paramètre /// </summary> /// Texte à inclure /// Chemin vers l'image /// Chemin vers l'image temporaire public void IncludeText(String strFilePath, String strTmpPath, String strInclude) { // Supprime le fichier temporaire si il existe if (File.Exists(strTmpPath)) { File.Delete(strTmpPath); } // Copie l'image dans le répertoire temporaire File.Copy(strFilePath, strTmpPath); // Crée une image depuis la class Bitmap Bitmap ImgFile = new Bitmap(strTmpPath); // Taille maximum du texte possible Int32 intMaxSize = ((ImgFile.Height - 1) * (ImgFile.Width - 1)) / 8; // Teste si le texte est trop long, si oui, affiche un message d'erreur if (strInclude.Length > intMaxSize) { System.Windows.MessageBox.Show("Le texte écrit est trop long !", "Erreur", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); return; } // Teste si l'encodage de l'image est le bon, si non, affiche un message d'erreur if (ImgFile.PixelFormat != PixelFormat.Format24bppRgb) { System.Windows.MessageBox.Show("Mauvais encodage de l'image !", "Erreur", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); return; } Int32 x; // Variable de boucle Byte byTemp = 0; // Variable temporaire Byte R = 0; // Couleur Rouge Byte G = 0; // Couleur Vert Byte B = 0; // Couleur Bleu Point ptCurrentPoint = Point.Empty; // Position actuelle dans l'image Color coCurrentColor = Color.Empty; // Couleur actuelle du pixel // Inscrit le carractère de fin strInclude += FIN; // Boucle pour inscrire le texte dans l'image for (x = 0; x < strInclude.Length; x++) { // Prend le premier caractère est le stock dans la variable temporaire byTemp = (Byte)strInclude[x]; // Stock la position du pixel selon l'avancement de l'écriture ptCurrentPoint.X = x % ImgFile.Width; ptCurrentPoint.Y = x / ImgFile.Width; // Séléctionne le pixel au point X;Y coCurrentColor = ImgFile.GetPixel(ptCurrentPoint.X, ptCurrentPoint.Y); // Change la couleur du pixel pour inscrire le carractère R = (Byte)(coCurrentColor.R & 248 | (byTemp & 7)); G = (Byte)(coCurrentColor.G & 248 | (byTemp & 56) >> 3); B = (Byte)(coCurrentColor.B & 252 | (byTemp & 192) >> 6); // Enregistre le nouveau pixel ImgFile.SetPixel(ptCurrentPoint.X, ptCurrentPoint.Y, Color.FromArgb(R, G, B)); } // Enregistre l'image try { ImgFile.Save(strFilePath); ImgFile.Dispose(); File.Delete(strTmpPath); } catch { System.Windows.MessageBox.Show("Erreur lors de l'enregistrement de l'image !", "Erreur", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); return; } } // Fin IncludeText
/// <summary> /// Inscrit dans l'image le texte donné en paramètre dans l'image donnée en paramètre /// </summary> /// Texte à inclure /// Chemin vers l'image /// Chemin vers l'image temporaire public void IncludeText(String strFilePath, String strTmpPath, String strInclude) { // Supprime le fichier temporaire si il existe if (File.Exists(strTmpPath)) { File.Delete(strTmpPath); } // Copie l'image dans le répertoire temporaire File.Copy(strFilePath, strTmpPath); // Crée une image depuis la class Bitmap using(Bitmap ImgFile = new Bitmap(strTmpPath)) { // Taille maximum du texte possible Int32 intMaxSize = ((ImgFile.Height - 1) * (ImgFile.Width - 1)) / 8; // Teste si le texte est trop long, si oui, affiche un message d'erreur if (strInclude.Length > intMaxSize) { System.Windows.MessageBox.Show("Le texte écrit est trop long !", "Erreur", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); return; } // Teste si l'encodage de l'image est le bon, si non, affiche un message d'erreur if (ImgFile.PixelFormat != PixelFormat.Format24bppRgb) { System.Windows.MessageBox.Show("Mauvais encodage de l'image !", "Erreur", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); return; } Int32 x; // Variable de boucle Byte byTemp = 0; // Variable temporaire Byte R = 0; // Couleur Rouge Byte G = 0; // Couleur Vert Byte B = 0; // Couleur Bleu Point ptCurrentPoint = Point.Empty; // Position actuelle dans l'image Color coCurrentColor = Color.Empty; // Couleur actuelle du pixel // Inscrit le carractère de fin strInclude += FIN; // Boucle pour inscrire le texte dans l'image for (x = 0; x < strInclude.Length; x++) { // Prend le premier caractère est le stock dans la variable temporaire byTemp = (Byte)strInclude[x]; // Stock la position du pixel selon l'avancement de l'écriture ptCurrentPoint.X = x % ImgFile.Width; ptCurrentPoint.Y = x / ImgFile.Width; // Séléctionne le pixel au point X;Y coCurrentColor = ImgFile.GetPixel(ptCurrentPoint.X, ptCurrentPoint.Y); // Change la couleur du pixel pour inscrire le carractère R = (Byte)(coCurrentColor.R & 248 | (byTemp & 7)); G = (Byte)(coCurrentColor.G & 248 | (byTemp & 56) >> 3); B = (Byte)(coCurrentColor.B & 252 | (byTemp & 192) >> 6); // Enregistre le nouveau pixel ImgFile.SetPixel(ptCurrentPoint.X, ptCurrentPoint.Y, Color.FromArgb(R, G, B)); } // Enregistre l'image try { ImgFile.Save(strFilePath); ImgFile.Dispose(); File.Delete(strTmpPath); } catch { System.Windows.MessageBox.Show("Erreur lors de l'enregistrement de l'image !", "Erreur", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); return; } } } // Fin IncludeText
Interception de System.Runtime.InteropServices.ExternalException Message=Une erreur générique s'est produite dans GDI+. Source=System.Drawing ErrorCode=-2147467259 StackTrace: à System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) à System.Drawing.Image.Save(String filename, ImageFormat format) à System.Drawing.Image.Save(String filename) à HideMe.BitmapFile.IncludeText(String strFilePath, String strTmpPath, String strInclude) InnerException:
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questiontry { ImgFile.Save(strFilePath); ImgFile.Dispose(); File.Delete(strTmpPath); } catch { System.Windows.MessageBox.Show("Erreur lors de l'enregistrement de l'image !", "Erreur", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); return; }