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);
}
}
} |
Partager