Advertisement
Guest User

bmp-class

a guest
Sep 22nd, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.11 KB | None | 0 0
  1.  private static Bitmap GetArgbCopy(Image sourceImage)
  2.         {
  3.             Bitmap bmpNew = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);
  4.  
  5.  
  6.             using (Graphics graphics = Graphics.FromImage(bmpNew))
  7.             {
  8.                 graphics.DrawImage(sourceImage, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), GraphicsUnit.Pixel);
  9.                 graphics.Flush();
  10.             }
  11.  
  12.  
  13.             return bmpNew;
  14.         }
  15.  
  16.         public static Bitmap CopyWithTransparency(this Image sourceImage, byte alphaComponent = 100)
  17.         {
  18.             Bitmap bmpNew = GetArgbCopy(sourceImage);
  19.             BitmapData bmpData = bmpNew.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
  20.  
  21.  
  22.             IntPtr ptr = bmpData.Scan0;
  23.  
  24.  
  25.             byte[] byteBuffer = new byte[bmpData.Stride * bmpNew.Height];
  26.  
  27.  
  28.             Marshal.Copy(ptr, byteBuffer, 0, byteBuffer.Length);
  29.  
  30.  
  31.             for (int k = 3; k < byteBuffer.Length; k += 4)
  32.             {
  33.                 byteBuffer[k] = alphaComponent;
  34.             }
  35.  
  36.  
  37.             Marshal.Copy(byteBuffer, 0, ptr, byteBuffer.Length);
  38.  
  39.  
  40.             bmpNew.UnlockBits(bmpData);
  41.  
  42.  
  43.             bmpData = null;
  44.             byteBuffer = null;
  45.  
  46.  
  47.             return bmpNew;
  48.         }
  49.  
  50.         public static Bitmap CopyAsGrayscale(this Image sourceImage)
  51.         {
  52.             Bitmap bmpNew = GetArgbCopy(sourceImage);
  53.             BitmapData bmpData = bmpNew.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
  54.  
  55.  
  56.             IntPtr ptr = bmpData.Scan0;
  57.  
  58.  
  59.             byte[] byteBuffer = new byte[bmpData.Stride * bmpNew.Height];
  60.  
  61.  
  62.             Marshal.Copy(ptr, byteBuffer, 0, byteBuffer.Length);
  63.  
  64.  
  65.             float rgb = 0;
  66.  
  67.  
  68.             for (int k = 0; k < byteBuffer.Length; k += 4)
  69.             {
  70.                 rgb = byteBuffer[k] * 0.11f;
  71.                 rgb += byteBuffer[k + 1] * 0.59f;
  72.                 rgb += byteBuffer[k + 2] * 0.3f;
  73.  
  74.  
  75.                 byteBuffer[k] = (byte)rgb;
  76.                 byteBuffer[k + 1] = byteBuffer[k];
  77.                 byteBuffer[k + 2] = byteBuffer[k];
  78.  
  79.  
  80.                 byteBuffer[k + 3] = 255;
  81.             }
  82.  
  83.  
  84.             Marshal.Copy(byteBuffer, 0, ptr, byteBuffer.Length);
  85.  
  86.  
  87.             bmpNew.UnlockBits(bmpData);
  88.  
  89.  
  90.             bmpData = null;
  91.             byteBuffer = null;
  92.  
  93.  
  94.             return bmpNew;
  95.         }
  96.  
  97.         public static Bitmap CopyAsSepiaTone(this Image sourceImage)
  98.         {
  99.             Bitmap bmpNew = GetArgbCopy(sourceImage);
  100.             BitmapData bmpData = bmpNew.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
  101.  
  102.  
  103.             IntPtr ptr = bmpData.Scan0;
  104.  
  105.  
  106.             byte[] byteBuffer = new byte[bmpData.Stride * bmpNew.Height];
  107.  
  108.  
  109.             Marshal.Copy(ptr, byteBuffer, 0, byteBuffer.Length);
  110.  
  111.  
  112.             byte maxValue = 255;
  113.             float r = 0;
  114.             float g = 0;
  115.             float b = 0;
  116.  
  117.  
  118.             for (int k = 0; k < byteBuffer.Length; k += 4)
  119.             {
  120.                 r = byteBuffer[k] * 0.189f + byteBuffer[k + 1] * 0.769f + byteBuffer[k + 2] * 0.393f;
  121.                 g = byteBuffer[k] * 0.168f + byteBuffer[k + 1] * 0.686f + byteBuffer[k + 2] * 0.349f;
  122.                 b = byteBuffer[k] * 0.131f + byteBuffer[k + 1] * 0.534f + byteBuffer[k + 2] * 0.272f;
  123.  
  124.  
  125.                 byteBuffer[k + 2] = (r > maxValue ? maxValue : (byte)r);
  126.                 byteBuffer[k + 1] = (g > maxValue ? maxValue : (byte)g);
  127.                 byteBuffer[k] = (b > maxValue ? maxValue : (byte)b);
  128.             }
  129.  
  130.  
  131.             Marshal.Copy(byteBuffer, 0, ptr, byteBuffer.Length);
  132.  
  133.  
  134.             bmpNew.UnlockBits(bmpData);
  135.  
  136.  
  137.             bmpData = null;
  138.             byteBuffer = null;
  139.  
  140.  
  141.             return bmpNew;
  142.         }
  143.  
  144.         public static Bitmap CopyAsNegative(this Image sourceImage)
  145.         {
  146.             Bitmap bmpNew = GetArgbCopy(sourceImage);
  147.             BitmapData bmpData = bmpNew.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
  148.  
  149.  
  150.             IntPtr ptr = bmpData.Scan0;
  151.  
  152.  
  153.             byte[] byteBuffer = new byte[bmpData.Stride * bmpNew.Height];
  154.  
  155.  
  156.             Marshal.Copy(ptr, byteBuffer, 0, byteBuffer.Length);
  157.             byte[] pixelBuffer = null;
  158.  
  159.  
  160.             int pixel = 0;
  161.  
  162.  
  163.             for (int k = 0; k < byteBuffer.Length; k += 4)
  164.             {
  165.                 pixel = ~BitConverter.ToInt32(byteBuffer, k);
  166.                 pixelBuffer = BitConverter.GetBytes(pixel);
  167.  
  168.  
  169.                 byteBuffer[k] = pixelBuffer[0];
  170.                 byteBuffer[k + 1] = pixelBuffer[1];
  171.                 byteBuffer[k + 2] = pixelBuffer[2];
  172.             }
  173.  
  174.  
  175.             Marshal.Copy(byteBuffer, 0, ptr, byteBuffer.Length);
  176.  
  177.  
  178.             bmpNew.UnlockBits(bmpData);
  179.  
  180.  
  181.             bmpData = null;
  182.             byteBuffer = null;
  183.  
  184.  
  185.             return bmpNew;
  186.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement