Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static Bitmap GetArgbCopy(Image sourceImage)
- {
- Bitmap bmpNew = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);
- using (Graphics graphics = Graphics.FromImage(bmpNew))
- {
- graphics.DrawImage(sourceImage, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), GraphicsUnit.Pixel);
- graphics.Flush();
- }
- return bmpNew;
- }
- public static Bitmap CopyWithTransparency(this Image sourceImage, byte alphaComponent = 100)
- {
- Bitmap bmpNew = GetArgbCopy(sourceImage);
- BitmapData bmpData = bmpNew.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
- IntPtr ptr = bmpData.Scan0;
- byte[] byteBuffer = new byte[bmpData.Stride * bmpNew.Height];
- Marshal.Copy(ptr, byteBuffer, 0, byteBuffer.Length);
- for (int k = 3; k < byteBuffer.Length; k += 4)
- {
- byteBuffer[k] = alphaComponent;
- }
- Marshal.Copy(byteBuffer, 0, ptr, byteBuffer.Length);
- bmpNew.UnlockBits(bmpData);
- bmpData = null;
- byteBuffer = null;
- return bmpNew;
- }
- public static Bitmap CopyAsGrayscale(this Image sourceImage)
- {
- Bitmap bmpNew = GetArgbCopy(sourceImage);
- BitmapData bmpData = bmpNew.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
- IntPtr ptr = bmpData.Scan0;
- byte[] byteBuffer = new byte[bmpData.Stride * bmpNew.Height];
- Marshal.Copy(ptr, byteBuffer, 0, byteBuffer.Length);
- float rgb = 0;
- for (int k = 0; k < byteBuffer.Length; k += 4)
- {
- rgb = byteBuffer[k] * 0.11f;
- rgb += byteBuffer[k + 1] * 0.59f;
- rgb += byteBuffer[k + 2] * 0.3f;
- byteBuffer[k] = (byte)rgb;
- byteBuffer[k + 1] = byteBuffer[k];
- byteBuffer[k + 2] = byteBuffer[k];
- byteBuffer[k + 3] = 255;
- }
- Marshal.Copy(byteBuffer, 0, ptr, byteBuffer.Length);
- bmpNew.UnlockBits(bmpData);
- bmpData = null;
- byteBuffer = null;
- return bmpNew;
- }
- public static Bitmap CopyAsSepiaTone(this Image sourceImage)
- {
- Bitmap bmpNew = GetArgbCopy(sourceImage);
- BitmapData bmpData = bmpNew.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
- IntPtr ptr = bmpData.Scan0;
- byte[] byteBuffer = new byte[bmpData.Stride * bmpNew.Height];
- Marshal.Copy(ptr, byteBuffer, 0, byteBuffer.Length);
- byte maxValue = 255;
- float r = 0;
- float g = 0;
- float b = 0;
- for (int k = 0; k < byteBuffer.Length; k += 4)
- {
- r = byteBuffer[k] * 0.189f + byteBuffer[k + 1] * 0.769f + byteBuffer[k + 2] * 0.393f;
- g = byteBuffer[k] * 0.168f + byteBuffer[k + 1] * 0.686f + byteBuffer[k + 2] * 0.349f;
- b = byteBuffer[k] * 0.131f + byteBuffer[k + 1] * 0.534f + byteBuffer[k + 2] * 0.272f;
- byteBuffer[k + 2] = (r > maxValue ? maxValue : (byte)r);
- byteBuffer[k + 1] = (g > maxValue ? maxValue : (byte)g);
- byteBuffer[k] = (b > maxValue ? maxValue : (byte)b);
- }
- Marshal.Copy(byteBuffer, 0, ptr, byteBuffer.Length);
- bmpNew.UnlockBits(bmpData);
- bmpData = null;
- byteBuffer = null;
- return bmpNew;
- }
- public static Bitmap CopyAsNegative(this Image sourceImage)
- {
- Bitmap bmpNew = GetArgbCopy(sourceImage);
- BitmapData bmpData = bmpNew.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
- IntPtr ptr = bmpData.Scan0;
- byte[] byteBuffer = new byte[bmpData.Stride * bmpNew.Height];
- Marshal.Copy(ptr, byteBuffer, 0, byteBuffer.Length);
- byte[] pixelBuffer = null;
- int pixel = 0;
- for (int k = 0; k < byteBuffer.Length; k += 4)
- {
- pixel = ~BitConverter.ToInt32(byteBuffer, k);
- pixelBuffer = BitConverter.GetBytes(pixel);
- byteBuffer[k] = pixelBuffer[0];
- byteBuffer[k + 1] = pixelBuffer[1];
- byteBuffer[k + 2] = pixelBuffer[2];
- }
- Marshal.Copy(byteBuffer, 0, ptr, byteBuffer.Length);
- bmpNew.UnlockBits(bmpData);
- bmpData = null;
- byteBuffer = null;
- return bmpNew;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement