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