static Color GetSinglePixel(BitmapImage source, int x, int y) { int stride = (source.Format.BitsPerPixel + 7) / 8; byte[] pixels = new byte[stride]; source.CopyPixels(new Int32Rect(x, y, 1, 1), pixels, stride, 0); return Color.FromArgb(pixels[3], pixels[0], pixels[1], pixels[2]); }
private void openButton_Click(object sender, RoutedEventArgs e) { var dlg = new OpenFileDialog(); if (dlg.ShowDialog(this).Value) { try { if (new FileInfo(dlg.FileName).Extension.ToLower() == ".bmpc") { var image = new ImageBMPC(dlg.FileName, false, null); var writable = new WriteableBitmap(image.Size.Width, image.Size.Height, 96, 96, PixelFormats.Bgra32, null); int stride = (image.Size.Width * 32 + 7) / 8; // Flip RB Color bits var data = new byte[image.Mipmaps[0].Data.LongLength]; image.Mipmaps[0].Data.CopyTo(data, 0); for (int i2 = 0; i2 != data.Length; i2 += 4) { byte c = data[i2]; data[i2] = data[i2+2]; data[i2+2] = c; } writable.WritePixels(new Int32Rect(0, 0, image.Size.Width, image.Size.Height), data, stride, 0); dstImage.Source = writable; } else { // Load binary data var bitmap = new BitmapImage(new Uri(dlg.FileName, UriKind.Absolute)); int stride = (bitmap.PixelWidth * bitmap.Format.BitsPerPixel + 7) / 8; srcData = new byte[bitmap.PixelWidth * stride]; dstData = new byte[bitmap.PixelWidth * stride]; bitmap.CopyPixels(srcData, stride, 0); bitmap.CopyPixels(dstData, stride, 0); // Get image atributes var ext = new FileInfo(dlg.FileName).Extension.ToLower(); width = bitmap.PixelWidth; height = bitmap.PixelHeight; // Display images var w = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight, 96, 96, PixelFormats.Bgra32, null); w.WritePixels(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), srcData, stride, 0); dstImage.Source = w; srcImage.Source = bitmap; processesImage(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
private void btnFilter_Click(object sender, RoutedEventArgs e) { if (_pointCollection[0].X != 0 || _pointCollection[_pointCollection.Count - 1].X != 255) { MessageBox.Show("You have to set the initial and final values of X!"); return; } if (imgOriginal.Source == null) { MessageBox.Show("You have to load the image first!"); return; } ImageSource ims = imgOriginal.Source; _bitmapImage = (BitmapImage)ims; _height = _bitmapImage.PixelHeight; _width = _bitmapImage.PixelWidth; _rawStride = (_width * _bitmapImage.Format.BitsPerPixel + 7) / 8; _pixelData = new byte[_rawStride * _height]; _bitmapImage.CopyPixels(_pixelData, _rawStride, 0); int[] function = new int[256]; for (int i = 0; i < 256; i++) { bool isKnown = false; foreach (Point p in _pointCollection) { if (i == (int)p.X) { isKnown = true; function[i] = (int)(255 - p.Y); break; } } if (isKnown) { continue; } for (int j = 0; j < _pointCollection.Count; j++) { if (_pointCollection[j].X < i && _pointCollection[j + 1].X >= i) { function[i] = 255 - ((int)_pointCollection[j].Y + ((int)_pointCollection[j + 1].Y - (int)_pointCollection[j].Y) * (i - (int)_pointCollection[j].X) / ((int)_pointCollection[j + 1].X - (int)_pointCollection[j].X)); break; } } } for (int y = 0; y < _height; y++) { int yIndex = y * _rawStride; for (int x = 0; x < _rawStride; x+=3) { if (x+yIndex+2 > _pixelData.Length-1) break; _pixelData[x + yIndex] = (byte)function[_pixelData[x + yIndex]]; _pixelData[x + yIndex + 1] = (byte)function[_pixelData[x + yIndex + 1]]; _pixelData[x + yIndex + 2] = (byte)function[_pixelData[x + yIndex + 2]]; } } _filteredBitmap = BitmapSource.Create(_width, _height, 96, 96, _bitmapImage.Format, _bitmapImage.Palette, _pixelData, _rawStride); imgFiltered.Source = _filteredBitmap; }
public List<string> GetHashCode(BitmapImage bitmap) {//takes a bitmap and translates it into the hashcode list List<string> hashCode= new List<string>(); int stride = bitmap.PixelWidth * (bitmap.Format.BitsPerPixel / 8); for (int i = 0; i < bitmap.PixelHeight; i++)//divides an image into rows { string row=""; for (int x = 0; x < bitmap.PixelWidth; x++)//iterates through each pixel in the row { byte[] pixel = new byte[bitmap.PixelHeight];//holds color values of a single pixel bitmap.CopyPixels(new Int32Rect(x, i, 1, 1), pixel, stride, 0);//assigns color values of a single pixel to the pixel array Color singlePixel = new Color();//creates new color objects and assigns the color values found in pixel array to it singlePixel.B = pixel[0]; singlePixel.G = pixel[1]; singlePixel.R = pixel[2]; singlePixel.A = pixel[3]; row += singlePixel.GetHashCode().ToString();//converst the color value into the hashcode and converts it to the string } hashCode.Add(row); if (i % 20 == 0)//Updates programm only after each tenth iteration, this makes program run a bit faster UpdatePRogress(); } return hashCode; }
public static System.Drawing.Bitmap BitmapImageToBitmap(BitmapImage source) { Bitmap bmp = new Bitmap(source.PixelWidth, source.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppRgb); BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); source.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data); return bmp; /* System.Drawing.Bitmap btm = null; int width = srs.PixelWidth; int height = srs.PixelHeight; int stride = width * ((srs.Format.BitsPerPixel + 7) / 8); byte[] bits = new byte[height * stride]; srs.CopyPixels(bits, stride, 0); unsafe { fixed (byte* pB = bits) { IntPtr ptr = new IntPtr(pB); btm = new System.Drawing.Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, ptr); } } return btm;*/ }
private byte[] ConvertBitmapImageToByteArray(BitmapImage bitmap) { int stride = bitmap.PixelWidth * 4; int size = bitmap.PixelHeight * stride; byte[] pixels = new byte[size]; bitmap.CopyPixels(pixels, stride, 0); return pixels; }
private byte[] ReadAllBytes(BitmapImage img) { var stride = img.PixelWidth * (img.Format.BitsPerPixel / 8); var pixels = new byte[stride * img.PixelHeight]; img.CopyPixels(pixels, stride, 0); return pixels; }
public static BitmapSource ConvertBitmapToOneTrueDpi(BitmapImage bitmapImage) { var width = bitmapImage.PixelWidth; var height = bitmapImage.PixelHeight; var stride = width * bitmapImage.Format.BitsPerPixel; var pixelData = new byte[stride * height]; bitmapImage.CopyPixels(pixelData, stride, 0); return BitmapSource.Create(width, height, OneTrueDpi, OneTrueDpi, bitmapImage.Format, null, pixelData, stride); }
/// <summary> /// Obtains the image data once it is loaded /// </summary> private void LoadPixelData() { var bitmapImage = new BitmapImage(new Uri("pack://application:,,,/Examples/HistogramDemo/hare.jpg")); int stride = ((bitmapImage.PixelWidth * bitmapImage.Format.BitsPerPixel) + 7) / 8; var pixelByteArray = new byte[bitmapImage.PixelHeight * stride]; bitmapImage.CopyPixels(pixelByteArray, stride, 0); this.pixelData = new int[pixelByteArray.Length / 4]; Buffer.BlockCopy(pixelByteArray, 0, this.pixelData, 0, pixelByteArray.Length); }
public ImageContext MakeBitmap() { BitmapImage myBitmapImage = new BitmapImage(); myBitmapImage.BeginInit(); myBitmapImage.UriSource = new Uri(@text1.Text); myBitmapImage.EndInit(); myImage.Source = myBitmapImage; ImageContext inSource = new ImageContext(myBitmapImage.PixelHeight, myBitmapImage.PixelWidth, myBitmapImage.Format); myBitmapImage.CopyPixels(inSource.PixelByteArray, inSource.NStride, 0); return inSource; }
public byte[] Load(string path, out int width, out int height, out int stride) { var source = new BitmapImage(new Uri("pack://application:,,,/" + path)); width = source.PixelWidth; height = source.PixelHeight; stride = 500; var bytes = new byte[stride * height]; source.CopyPixels(bytes, stride, 0); return bytes; }
internal static BitmapSource ConvertBitmapTo96Dpi(BitmapImage bitmap_image) { const double dpi = 96; var width = bitmap_image.PixelWidth; var height = bitmap_image.PixelHeight; var stride = (bitmap_image.Format.BitsPerPixel * width + 7) / 8; var pixel_data = new byte[stride * height]; bitmap_image.CopyPixels(pixel_data, stride, 0); var rtn = BitmapSource.Create(width, height, dpi, dpi, bitmap_image.Format, null, pixel_data, stride); if (rtn.CanFreeze) rtn.Freeze(); return rtn; }
private void ProcessImage(BitmapImage bmp) { byte[] pixels = new byte[bmp.PixelWidth * bmp.PixelHeight * 4]; bmp.CopyPixels(pixels, bmp.PixelWidth * 4, 0); for (int i = 0; i < pixels.Length; ) { //BGRA blues[pixels[i++]]++; greens[pixels[i++]]++; reds[pixels[i++]]++; i++; } CreateHistograms(); }
public void MakeBitmap() { BitmapImage myBitmapImage = new BitmapImage(); myBitmapImage.BeginInit(); myBitmapImage.UriSource = new Uri(@text1.Text); myBitmapImage.EndInit(); myImage.Source = myBitmapImage; // height = myBitmapImage.PixelHeight; width = myBitmapImage.PixelWidth; imageFormat = myBitmapImage.Format; nStride = (myBitmapImage.PixelWidth * imageFormat.BitsPerPixel + 7) / 8; pixelByteArraySize = myBitmapImage.PixelHeight * nStride; pixelByteArrayIn = new byte[pixelByteArraySize]; myBitmapImage.CopyPixels(pixelByteArrayIn, nStride, 0); }
private void LoadImage() { this.originalImage = new BitmapImage(); originalImage.BeginInit(); originalImage.UriSource = new Uri(FilePath.Text, UriKind.Absolute); originalImage.EndInit(); OriginalImage.Source = originalImage; ModifiedImage.Source = originalImage; int[] pixelData = new int[originalImage.PixelHeight*originalImage.PixelWidth]; int widthInByte = 4*originalImage.PixelWidth; originalImage.CopyPixels(pixelData, widthInByte, 0); DrawHistogram(pixelData, "ORIGINAL"); DrawHistogram(pixelData, "MODIFIED"); }
public WinFormsIcon(BitmapImage image) { var width = image.PixelWidth; var height = image.PixelHeight; var stride = width * ((image.Format.BitsPerPixel + 7) / 8); IntPtr ptr = Marshal.AllocHGlobal(height * stride); try { image.CopyPixels(new Int32Rect(0, 0, width, height), ptr, height * stride, stride); using (var bitmap = new Bitmap(width, height, stride, PixelFormat.Format32bppArgb, ptr)) { this.winFormsIcon = Icon.FromHandle(bitmap.GetHicon()); } } finally { Marshal.FreeHGlobal(ptr); } }
public static Color AverageColorOfImage(BitmapImage bmp) { int stride = bmp.PixelWidth*4; int size = bmp.PixelHeight*stride; byte[] pixels = new byte[size]; bmp.CopyPixels(pixels, stride, 0); int totalR = 0; int totalG = 0; int totalB = 0; int minDiversion = 15; // drop pixels that do not differ by at least minDiversion between color values (white, gray or black) int dropped = 0; // keep track of dropped pixels for (int y = 0; y < bmp.PixelHeight; y++) { for (int x = 0; x < bmp.PixelWidth; x++) { int index = y * stride + 4 * x; byte red = pixels[index]; byte green = pixels[index + 1]; byte blue = pixels[index + 2]; //byte alpha = pixels[index + 3]; if ((Math.Abs(red - green) > minDiversion || Math.Abs(red - blue) > minDiversion || Math.Abs(green - blue) > minDiversion) && (red + green + blue) < 600 && (red + green + blue) > 100) { totalR += red; totalG += green; totalB += blue; } else { dropped++; } } } int count = size - dropped; return Color.Multiply(Color.FromRgb((byte)(totalR / count), (byte)(totalG / count), (byte)(totalB / count)), 6); }
private double[] GetPixelData(string path) { BitmapSource img = new BitmapImage(new System.Uri(path)); int stride = img.PixelWidth * 4; int size = img.PixelHeight * stride; byte[] pixels = new byte[size]; img.CopyPixels(pixels, stride, 0); var intensities = new double[img.PixelWidth * img.PixelHeight]; for(int i = 0; i < img.PixelWidth; i++) { for(int j = 0; j < img.PixelHeight; j++) { var index = i * img.PixelWidth + j; intensities[index] = (pixels[index * 4] + pixels[index * 4 + 1] + pixels[index * 4 + 2]) / 3; } } return intensities; }
protected override void Update(GameTime gameTime) { base.Update(gameTime); if(_mjpeg.CurrentFrame != null) { BitmapImage img = new BitmapImage(); img.BeginInit(); img.StreamSource = new MemoryStream(_mjpeg.CurrentFrame); img.EndInit(); int stride = (img.PixelWidth * img.Format.BitsPerPixel + 7) / 8; byte[] frameBuffer = new byte[img.PixelHeight * stride]; img.CopyPixels(frameBuffer, stride, 0); if(_texture == null) _texture = new Texture2D(this.GraphicsDevice, img.PixelWidth, img.PixelHeight); _texture.SetData(frameBuffer); } }
private void ProcessImage(BitmapImage bmp) { byte[] pixels = new byte[bmp.PixelWidth * bmp.PixelHeight * 4]; bmp.CopyPixels(pixels, bmp.PixelWidth * 4, 0); for (int i = 0; i < pixels.Length; ) { //BGRA blues[pixels[i++]]++; greens[pixels[i++]]++; reds[pixels[i++]]++; i++; } EnumerableDataSource<int> x = new EnumerableDataSource<int>(Enumerable.Range(0, 256).ToArray()); x.SetXMapping(_x => _x); Func<int, double> mapping; if (check.IsChecked.GetValueOrDefault()) mapping = logMapping; else mapping = linearMapping; red = new EnumerableDataSource<int>(reds); red.SetYMapping(mapping); green = new EnumerableDataSource<int>(greens); green.SetYMapping(mapping); blue = new EnumerableDataSource<int>(blues); blue.SetYMapping(mapping); CompositeDataSource rDS = new CompositeDataSource(x, red); CompositeDataSource gDS = new CompositeDataSource(x, green); CompositeDataSource bDS = new CompositeDataSource(x, blue); plotter.RemoveUserElements(); plotter.AddLineGraph(rDS, Colors.Red, 1, "Red").FilteringEnabled = false; plotter.AddLineGraph(gDS, Colors.Green, 1, "Green").FilteringEnabled = false; plotter.AddLineGraph(bDS, Colors.Blue, 1, "Blue").FilteringEnabled = false; }
public static BitmapSource ConvertBitmapTo96DPI(BitmapImage bitmapImage) { try { double dpi = 96; int width = bitmapImage.PixelWidth; int height = bitmapImage.PixelHeight; int stride = width * 4; // 4 bytes per pixel byte[] pixelData = new byte[stride * height]; bitmapImage.CopyPixels(pixelData, stride, 0); return BitmapSource.Create(width, height, dpi, dpi, PixelFormats.Bgra32, null, pixelData, stride); } catch (System.Exception ex1) { TaskDialog.Show("Error!", "exception: " + ex1); } return null; }
private void LoadImage() { BitmapImage img = new BitmapImage(new Uri("..//..//Images//Happy.bmp", UriKind.Relative)); // BitmapImage img = new BitmapImage(new Uri("..//..//Images//Mad.png", UriKind.Relative)); img.CreateOptions = BitmapCreateOptions.None; stride = img.PixelWidth * 4; int size = img.PixelHeight * stride; byte[] pixels = new byte[size]; img.CopyPixels(pixels, stride, 0); sil = pixels; /* BitmapSource bms = Happy.Source as BitmapSource; WriteableBitmap wb = new WriteableBitmap(bms); for (int i = 300; i < 400; ++i) { for (int j = 200; j < 300; ++j) { //Console.WriteLine(i + "\t" + j); Color color = getPixelColor(wb, i, j); //Console.WriteLine(color); if (color == Colors.White) { Console.WriteLine(i + "\t" + j); Console.WriteLine("Found white!"); } } } * */ }
// Load the image byte[] LoadImage(string filename) { var myBitmapImage = new BitmapImage(); myBitmapImage.BeginInit(); myBitmapImage.UriSource = new Uri(filename); _tilePixelHeight = myBitmapImage.DecodePixelHeight = _tilePixelHeight; _tilePixelWidth = myBitmapImage.DecodePixelWidth = _tilePixelWidth; myBitmapImage.EndInit(); _format = myBitmapImage.Format; var stride = (int)myBitmapImage.Width * 4; var dest = new byte[stride * _tilePixelHeight]; myBitmapImage.CopyPixels(dest, stride, 0); return dest; }
void load() { FormsBitmap = new System.Drawing.Bitmap(path.LocalPath); img = new BitmapImage(path); source = img; width = (int)FormsBitmap.Width; height = (int)FormsBitmap.Height; rawStride = (width * img.Format.BitsPerPixel + 7) / 8; pixelData = new byte[height * rawStride]; img.CopyPixels(pixelData, rawStride, 0); pf = img.Format; last.FoImage = FormsBitmap; last.img = img; }
public void setBitmap(BitmapImage b,Bitmap bb) { FormsBitmap = bb; img = b; width = (int)img.Width; height = (int)img.Height; rawStride = (width * img.Format.BitsPerPixel + 7) / 8; pixelData = new byte[height * rawStride]; img.CopyPixels(pixelData, rawStride, 0); pf = img.Format; }
private void ProcessFace() { FacePhotoAlbumContext context = new FacePhotoAlbumContext(); foreach (var row in context.T_PhotoInfo.Where(p => p.Status == 99).Join(context.T_Face.Where(q => q.Status != 99) , p => new { PhotoNum = p.PhotoNum } , q => new { PhotoNum = q.PhotoNum } , (p, q) => new { T_FaceComparison = p, T_Face = q })) { row.T_Face.Status = 99; } int ret = HiwitLib.InitSDK(AppDomain.CurrentDomain.BaseDirectory); var photoInfoRows = context.T_PhotoInfo.Where(p => p.Status == 0); photoInfoRows.ToList().ForEach(photoInfoRow => { string photoFilePath = Path.Combine(photoInfoRow.FilePath, photoInfoRow.FileName); byte[] byteImage = File.ReadAllBytes(photoFilePath); int faceNumber = 10; HiwitLib.HIWITFaceRegion[] faceRegion = new HiwitLib.HIWITFaceRegion[faceNumber]; for (int i = 0; i < faceNumber; i++) faceRegion[i].faceVertex = new HiwitLib.HIWITPoint[4]; ret = HiwitLib.FaceDetect(byteImage, faceRegion, ref faceNumber, 0, 0, null); if (ret != HiwitLib.HIWIT_ERR_NONE) throw new ArgumentException("FaceDetect fail:" + ret); if (faceNumber == 0) { photoInfoRow.Status = 2; photoInfoRow.UpdateTime = DateTime.Now; } else { BitmapImage sourceImage = new BitmapImage(); sourceImage.BeginInit(); sourceImage.UriSource = new Uri(photoFilePath); sourceImage.EndInit(); int imageWidth = sourceImage.PixelWidth; int imageHeight = sourceImage.PixelHeight; int bitsPerPixel = sourceImage.Format.BitsPerPixel; for (int i = 0; i < faceNumber; i++) { int left = (int)faceRegion[i].faceVertex.Min(p => p.x); int right = (int)faceRegion[i].faceVertex.Max(p => p.x); int top = (int)faceRegion[i].faceVertex.Min(p => p.y); int bottom = (int)faceRegion[i].faceVertex.Max(p => p.y); left = left < 0 ? 0 : (left > imageWidth ? imageWidth : left); right = right < 0 ? 0 : (right > imageWidth ? imageWidth : right); top = top < 0 ? 0 : (top > imageHeight ? imageHeight : top); bottom = bottom < 0 ? 0 : (bottom > imageHeight ? imageHeight : bottom); var newRow = new T_Face(); newRow.PhotoNum = photoInfoRow.PhotoNum; newRow.SequenceNum = i; newRow.RectLeft = left; newRow.RectRight = right; newRow.RectTop = top; newRow.RectBottom = bottom; newRow.ConfirmAlbumNum = -1; newRow.PossibleAlbumNum = "-1"; //定义切割矩形 var cut = new Int32Rect(left, top, right - left, bottom - top); //计算Stride var stride = sourceImage.Format.BitsPerPixel * cut.Width / 8; //声明字节数组 byte[] data = new byte[cut.Height * stride]; //调用CopyPixels sourceImage.CopyPixels(cut, data, stride, 0); BitmapSource desImage = BitmapSource.Create(cut.Width, cut.Height, 96, 96, sourceImage.Format, null, data, stride); var bitmapEncoder = new PngBitmapEncoder(); bitmapEncoder.Frames.Add(BitmapFrame.Create(desImage)); string faceFileNmae = Guid.NewGuid().ToString() + ".png"; newRow.FaceFileName = faceFileNmae; newRow.FaceFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Faces"); using (FileStream file = new FileStream(Path.Combine(newRow.FaceFilePath, newRow.FaceFileName), FileMode.Create)) bitmapEncoder.Save(file); HiwitLib.HIWITFaceInfo faceInfo = new HiwitLib.HIWITFaceInfo(); faceInfo.faceContour = new HiwitLib.HIWITPoint[21]; faceInfo.leftEyeBrowContour = new HiwitLib.HIWITPoint[8]; faceInfo.leftEyeContour = new HiwitLib.HIWITPoint[8]; faceInfo.rightEyeContour = new HiwitLib.HIWITPoint[8]; faceInfo.rightEyeBrowContour = new HiwitLib.HIWITPoint[8]; faceInfo.mouthContour = new HiwitLib.HIWITPoint[22]; faceInfo.noseContour = new HiwitLib.HIWITPoint[13]; ret = HiwitLib.FaceAlignmentFromFaceRegion(byteImage, faceRegion[i], ref faceInfo); if (ret == HiwitLib.HIWIT_ERR_NONE) { int size = Marshal.SizeOf(faceInfo); byte[] byteFaceInfo = new byte[size]; StructComversion.StructToBytes(faceInfo, byteFaceInfo, 0, size); newRow.Status = 0; newRow.FeatureData = byteFaceInfo; } else { newRow.FeatureData = new byte[0]; newRow.Status = 2; } newRow.UpdateTime = DateTime.Now; context.T_Face.Add(newRow); } photoInfoRow.Status = 1; context.SaveChanges(); } }); HiwitLib.UnInitSDK(); }
public static BitmapSource LoadPokemonImageFromFile(ushort dexID, bool isShiny, byte formID = byte.MaxValue) { string path = Path.Combine(PokeManager.ApplicationDirectory, "Resources/Pokemon/" + (isShiny ? "FrontShiny" : "Front")); string file = null; if (formID != byte.MaxValue) { if (File.Exists(Path.Combine(path, "Forms", dexID.ToString() + "-" + formID.ToString() + ".png"))) file = Path.Combine(path, "Forms", dexID.ToString() + "-" + formID.ToString() + ".png"); else if (File.Exists(Path.Combine(path, "Forms", dexID.ToString("000") + "-" + formID.ToString("00") + ".png"))) file = Path.Combine(path, "Forms", dexID.ToString("000") + "-" + formID.ToString("00") + ".png"); else return null; } else if (dexID == 387 && File.Exists(Path.Combine(path, "Egg.png"))) file = Path.Combine(path, "Egg.png"); else if (File.Exists(Path.Combine(path, dexID.ToString() + ".png"))) file = Path.Combine(path, dexID.ToString() + ".png"); else if (File.Exists(Path.Combine(path, dexID.ToString("000") + ".png"))) file = Path.Combine(path, dexID.ToString("000") + ".png"); else return null; BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache; bitmap.UriSource = new Uri(file); bitmap.EndInit(); double dpi = gen3PokemonImages[1].Image.DpiX; int stride = (bitmap.PixelWidth * bitmap.Format.BitsPerPixel + 7) / 8; byte[] pixelData = new byte[stride * bitmap.PixelHeight]; bitmap.CopyPixels(pixelData, stride, 0); BitmapSource bitmapSource = BitmapSource.Create(bitmap.PixelWidth, bitmap.PixelHeight, dpi, dpi, bitmap.Format, bitmap.Palette, pixelData, stride); return bitmapSource; }
/// <summary> /// Returns the BitMapImage of the cover image /// </summary> /// <param name="imageUri">URI of the cover image</param> /// <returns>Bitmap image of the cover</returns> static async Task<BitmapSource> GetBitmapImage(Uri imageUri) { var request = WebRequest.CreateHttp(imageUri); var ms = new MemoryStream(); var response = await request.GetResponseAsync(); await response.GetResponseStream().CopyToAsync(ms); ms.Seek(0, SeekOrigin.Begin); var image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = ms; image.EndInit(); image.Freeze(); int stride = (image.Format.BitsPerPixel / 8) * image.PixelWidth; var pixels = new byte[stride * image.PixelHeight]; image.CopyPixels(pixels, stride, 0); var average = pixels.Average(b => (decimal?)b); if (average < 20 || average > 230) { return null; } else { return image; } }
private static ImageSource MakeImageSourceFromFile(string fname) { BitmapImage bi = new BitmapImage(); Uri _source = new Uri(fname); bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnLoad; bi.UriSource = _source; bi.EndInit(); //bi.Freeze(); int width = bi.PixelWidth; int height = bi.PixelHeight; //int stride = width / 8; PixelFormat pf = bi.Format; int stride = (width * pf.BitsPerPixel + 7) / 8; byte[] pixels = new byte[height * stride]; bi.CopyPixels(pixels, stride, 0); BitmapSource bms = BitmapSource.Create(width, height, bi.DpiX, bi.DpiY, pf, bi.Palette, pixels, stride); //return bi; return bms; }
private void ScanGemImage(BitmapImage bitmap) { if (Settings.Instance.GemColorInfo.ContainsKey(this.Name) == false) { int stride = bitmap.PixelWidth * 4; int size = bitmap.PixelHeight * stride; byte[] pixels = new byte[size]; bitmap.CopyPixels(pixels, stride, 0); int rHue = 356; int gHue = 100; int bHue = 220; int hueThreshold = 10; float rCount = 0; float gCount = 0; float bCount = 0; for (int i = 0; i < pixels.Length; i += 4) { int alpha = pixels[i + 3]; if (alpha > 0) { int r = pixels[i + 2]; int g = pixels[i + 1]; int b = pixels[i]; System.Drawing.Color c = System.Drawing.Color.FromArgb(r, g, b); float colorHue = c.GetHue(); if (c.GetSaturation() > 0.4f) { if (IsCloseTo(colorHue, rHue, hueThreshold)) rCount++; else if (IsCloseTo(colorHue, gHue, hueThreshold)) gCount++; else if (IsCloseTo(colorHue, bHue, hueThreshold)) bCount++; } } } GemRequirement gemReq = POEStashSorterModels.GemRequirement.None; if (rCount > bCount && rCount > gCount) gemReq = POEStashSorterModels.GemRequirement.Str; else if (bCount > rCount && bCount > gCount) gemReq = POEStashSorterModels.GemRequirement.Int; else // (gCount > rCount && gCount > bCount) gemReq = POEStashSorterModels.GemRequirement.Dex; Settings.Instance.GemColorInfo.Add(this.FullItemName, gemReq); Settings.Instance.SaveChanges(); } }