public static string GetImageHash(ImageModel source) { // Init of variables List <byte> colorList = new List <byte>(); string hash; int i, j; // Create new image with 16x16 pixels Bitmap bmpMin = new Bitmap(source.GetBitmapImage(), new Size(16, 16)); // Loop through the 16x16 image and add each pixel to the list for (j = 0; j < bmpMin.Height; j++) { for (i = 0; i < bmpMin.Width; i++) { colorList.Add(bmpMin.GetPixel(i, j).R); } } // Convert the pixel list into a SHA1 hash SHA1Managed sha = new SHA1Managed(); byte[] checksum = sha.ComputeHash(colorList.ToArray()); hash = BitConverter.ToString(checksum).Replace("-", string.Empty); // Get rid of the old resources sha.Dispose(); bmpMin.Dispose(); // Return the hash return(hash); }
public ImageModel ApplyBlackWhiteFilter(ImageModel image) { Bitmap bmp = image.GetBitmapImage(); int rgb; Color c; for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { c = bmp.GetPixel(x, y); rgb = ((c.R + c.G + c.B) / 3); bmp.SetPixel(x, y, Color.FromArgb(rgb, rgb, rgb)); } } return(new ImageModel(bmp, image.name)); }
//apply filter that swaps all pixel colors public ImageModel ApplySwapFilter(ImageModel image) { Color c; Bitmap bmp = image.GetBitmapImage(); for (int i = 0; i < bmp.Width; i++) { for (int x = 0; x < bmp.Height; x++) { c = bmp.GetPixel(i, x); Color cLayer = Color.FromArgb(c.A, c.G, c.B, c.R); bmp.SetPixel(i, x, cLayer); } } return(new ImageModel(bmp, image.name)); }
// Creates a square bitmap for displaying in the app public ImageModel GetPreviewImage(int width) { // TODO : replace this with something better? float ratio = 1.0f; Bitmap bitmapResult; int maxSide; // Correct image dimensions for preview in app if (image.GetBitmapImage().Width > image.GetBitmapImage().Height) { maxSide = image.GetBitmapImage().Width; ratio = (float)maxSide / (float)width; bitmapResult = new Bitmap(width, (int)(image.GetBitmapImage().Height / ratio)); } else { maxSide = image.GetBitmapImage().Height; ratio = (float)maxSide / (float)width; bitmapResult = new Bitmap((int)(image.GetBitmapImage().Width / ratio), width); } using (Graphics graphicsResult = Graphics.FromImage(bitmapResult)) { graphicsResult.CompositingQuality = CompositingQuality.HighQuality; graphicsResult.InterpolationMode = InterpolationMode.HighQualityBicubic; graphicsResult.PixelOffsetMode = PixelOffsetMode.HighQuality; graphicsResult.DrawImage(image.GetBitmapImage(), new Rectangle(0, 0, bitmapResult.Width, bitmapResult.Height), new Rectangle(0, 0, image.GetBitmapImage().Width, image.GetBitmapImage().Height), GraphicsUnit.Pixel); graphicsResult.Flush(); } return(new ImageModel(bitmapResult, image.name)); }
// Apply the rainbow filter public ImageModel ApplyRainbowFilter(ImageModel image) { try { Bitmap bmp = image.GetBitmapImage(); Bitmap result = new Bitmap(bmp.Width, bmp.Height); int raz = bmp.Height / 4; for (int i = 0; i < bmp.Width; i++) { for (int x = 0; x < bmp.Height; x++) { if (i < (raz)) { result.SetPixel(i, x, Color.FromArgb(bmp.GetPixel(i, x).R / 5, bmp.GetPixel(i, x).G, bmp.GetPixel(i, x).B)); } else if (i < (raz * 2)) { result.SetPixel(i, x, Color.FromArgb(bmp.GetPixel(i, x).R, bmp.GetPixel(i, x).G / 5, bmp.GetPixel(i, x).B)); } else if (i < (raz * 3)) { result.SetPixel(i, x, Color.FromArgb(bmp.GetPixel(i, x).R, bmp.GetPixel(i, x).G, bmp.GetPixel(i, x).B / 5)); } else if (i < (raz * 4)) { result.SetPixel(i, x, Color.FromArgb(bmp.GetPixel(i, x).R / 5, bmp.GetPixel(i, x).G, bmp.GetPixel(i, x).B / 5)); } else { result.SetPixel(i, x, Color.FromArgb(bmp.GetPixel(i, x).R / 5, bmp.GetPixel(i, x).G / 5, bmp.GetPixel(i, x).B / 5)); } } } return(new ImageModel(result, image.name)); } catch (NullReferenceException) { } return(null); }
private ImageModel ConvolutionFilter( ImageModel source, double[,] filterMatrix, double factor = 1, int bias = 0, bool grayscale = false) { Bitmap sourceBitmap = source.GetBitmapImage(); BitmapData sourceData = sourceBitmap.LockBits( new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); byte[] pixelBuffer = new byte[sourceData.Stride * sourceData.Height]; byte[] resultBuffer = new byte[sourceData.Stride * sourceData.Height]; Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length); sourceBitmap.UnlockBits(sourceData); int filterWidth = filterMatrix.GetLength(1); int filterHeight = filterMatrix.GetLength(0); int filterOffset = (filterWidth - 1) / 2; for (int offsetY = filterOffset; offsetY < sourceBitmap.Height - filterOffset; offsetY++) { for (int offsetX = filterOffset; offsetX < sourceBitmap.Width - filterOffset; offsetX++) { double blue = 0; double green = 0; double red = 0; int byteOffset = offsetY * sourceData.Stride + offsetX * 4; for (int filterY = -filterOffset; filterY <= filterOffset; filterY++) { for (int filterX = -filterOffset; filterX <= filterOffset; filterX++) { int calcOffset = byteOffset + (filterX * 4) + (filterY * sourceData.Stride); blue += pixelBuffer[calcOffset] * filterMatrix[filterY + filterOffset, filterX + filterOffset]; green += pixelBuffer[calcOffset + 1] * filterMatrix[filterY + filterOffset, filterX + filterOffset]; red += pixelBuffer[calcOffset + 2] * filterMatrix[filterY + filterOffset, filterX + filterOffset]; } } // Get RGB totals blue = factor * blue + bias; green = factor * green + bias; red = factor * red + bias; // Validate RGB totals blue = (blue > 255 ? 255 : blue < 0 ? 0 : blue); green = (green > 255 ? 255 : green < 0 ? 0 : green); red = (red > 255 ? 255 : red < 0 ? 0 : red); resultBuffer[byteOffset] = (byte)(blue); resultBuffer[byteOffset + 1] = (byte)(green); resultBuffer[byteOffset + 2] = (byte)(red); resultBuffer[byteOffset + 3] = 255; } } Bitmap resultBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height); BitmapData resultData = resultBitmap.LockBits( new Rectangle(0, 0, resultBitmap.Width, resultBitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); Marshal.Copy(resultBuffer, 0, resultData.Scan0, resultBuffer.Length); resultBitmap.UnlockBits(resultData); return(new ImageModel(resultBitmap, source.name)); }
public static ImageModel ConvolutionFilter( ImageModel source, double[,] xFilterMatrix, double[,] yFilterMatrix, bool grayscale = false) { Bitmap sourceBitmap = source.GetBitmapImage(); BitmapData sourceData = sourceBitmap.LockBits( new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); byte[] pixelBuffer = new byte[sourceData.Stride * sourceData.Height]; byte[] resultBuffer = new byte[sourceData.Stride * sourceData.Height]; Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length); sourceBitmap.UnlockBits(sourceData); int filterOffset = 1; for (int offsetY = filterOffset; offsetY < sourceBitmap.Height - filterOffset; offsetY++) { for (int offsetX = filterOffset; offsetX < sourceBitmap.Width - filterOffset; offsetX++) { double blueX, greenX, redX; blueX = greenX = redX = 0.0; double blueY, greenY, redY; blueY = greenY = redY = 0.0; double blueTotal, greenTotal, redTotal; int byteOffset = offsetY * sourceData.Stride + offsetX * 4; for (int filterY = -filterOffset; filterY <= filterOffset; filterY++) { for (int filterX = -filterOffset; filterX <= filterOffset; filterX++) { int calcOffset = byteOffset + (filterX * 4) + (filterY * sourceData.Stride); blueX += pixelBuffer[calcOffset] * xFilterMatrix[filterY + filterOffset, filterX + filterOffset]; greenX += pixelBuffer[calcOffset + 1] * xFilterMatrix[filterY + filterOffset, filterX + filterOffset]; redX += pixelBuffer[calcOffset + 2] * xFilterMatrix[filterY + filterOffset, filterX + filterOffset]; blueY += pixelBuffer[calcOffset] * yFilterMatrix[filterY + filterOffset, filterX + filterOffset]; greenY += pixelBuffer[calcOffset + 1] * yFilterMatrix[filterY + filterOffset, filterX + filterOffset]; redY += pixelBuffer[calcOffset + 2] * yFilterMatrix[filterY + filterOffset, filterX + filterOffset]; } } // Get totals of each color blueTotal = Math.Sqrt((blueX * blueX) + (blueY * blueY)); greenTotal = Math.Sqrt((greenX * greenX) + (greenY * greenY)); redTotal = Math.Sqrt((redX * redX) + (redY * redY)); // Check totals are valid if (blueTotal > 255) { blueTotal = 255; } if (greenTotal > 255) { greenTotal = 255; } if (redTotal > 255) { redTotal = 255; } resultBuffer[byteOffset] = (byte)(blueTotal); resultBuffer[byteOffset + 1] = (byte)(greenTotal); resultBuffer[byteOffset + 2] = (byte)(redTotal); resultBuffer[byteOffset + 3] = 255; } } Bitmap resultBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height); BitmapData resultData = resultBitmap.LockBits( new Rectangle(0, 0, resultBitmap.Width, resultBitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); Marshal.Copy(resultBuffer, 0, resultData.Scan0, resultBuffer.Length); resultBitmap.UnlockBits(resultData); return(new ImageModel(resultBitmap, source.name)); }