public void Draw(MagickImage map) { MainForm.ProgressStart("Drawing lightmap ..."); // Get the heightmap MagickImage heightmap = zoneConfiguration.Heightmap.Heightmap; using (MagickImage lightmap = new MagickImage(Color.Transparent, 256, 256)) { using (PixelCollection heightmapPixels = heightmap.GetReadOnlyPixels()) { using (WritablePixelCollection lightmapPixels = lightmap.GetWritablePixels()) { // z-component of surface normals double nz = 512d / zScale; double nz_2 = nz * nz; double nzlz = nz * lightVector[2]; int y1 = 0, y2 = 0; for (int y = 0; y < lightmap.Height; y++) { if (y == 0) y1 = 0; else y1 = y - 1; if (y == 255) y2 = 255; else y2 = y + 1; int x1 = 0, x2 = 0; for (int x = 0; x < lightmap.Width; x++) { if (x == 0) x1 = 0; else x1 = x - 1; if (x == 255) x2 = 255; else x2 = x + 1; double l = heightmapPixels.GetPixel(x1, y).GetChannel(0); double r = heightmapPixels.GetPixel(x2, y).GetChannel(0); double u = heightmapPixels.GetPixel(x, y1).GetChannel(0); double d = heightmapPixels.GetPixel(x, y2).GetChannel(0); double nx = l - r; double ny = u - d; double m_normal = Math.Sqrt(nx * nx + ny * ny + nz_2); double ndotl = (nx * lightVector[0] + ny * lightVector[1] + nzlz) / m_normal; double pixelValue = lightBase - ndotl * lightScale * 256d; ushort pixelValueDiff = 0; ushort alphaValue = ushort.MaxValue; if(pixelValue < 0) { pixelValueDiff = 0; alphaValue = (ushort)pixelValue; } else { pixelValueDiff = (ushort)pixelValue; } // ColorDodge map // white lightens areas where black does nothing // alpha darkens areas lightmapPixels.Set(x, y, new ushort[] { pixelValueDiff, pixelValueDiff, pixelValueDiff, alphaValue }); } int percent = 100 * y / lightmap.Height; MainForm.ProgressUpdate(percent); } } } MainForm.ProgressStartMarquee("Merging..."); lightmap.Blur(0.0, 0.5); lightmap.VirtualPixelMethod = VirtualPixelMethod.Transparent; lightmap.FilterType = FilterType.Gaussian; lightmap.Resize(zoneConfiguration.TargetMapSize, zoneConfiguration.TargetMapSize); // Apply the bumpmap using ColorDodge map.Composite(lightmap, 0, 0, CompositeOperator.ColorDodge); MainForm.ProgressReset(); } }
public static List<string> PointGenerator(string inputBitmapPath, PixFormat _pixFor) { List<string> imglist = new List<string>(); try { if (_pixFor == PixFormat._null) throw new Exception("Format null olamaz") { Source = "" }; using (MagickImage imagem = new MagickImage(inputBitmapPath)) { //############# ResizeImage ############# int yuzde = 100; imagem.Quality = 100; int _w = imagem.Width + (imagem.Width / 100) * yuzde; int _h = imagem.Height + (imagem.Height / 100) * yuzde; int new_W = _w - (_w % (int)_pixFor); int new_H = _h - (_h % (int)_pixFor); imagem.Resize(new_W, new_H); imagem.Blur(5, 5); //############# GenerateSquare ############# /////////// calculate image point rgb avg //////////// string curImgPth = Path.GetFileName(inputBitmapPath); string path = UserProperty.PixelXmlMap_Path + "\\" + curImgPth.Substring(0, curImgPth.Length - 4) + _pixFor.ToString() + "_.xml"; FileInfo finf = new FileInfo(path); List<ImgSquare> sp0 = new List<ImgSquare>(); if (!File.Exists(path)) { int[,] pixavg = new int[5, 3]; int _pixformat = (int)_pixFor; WritablePixelCollection _totalpix = imagem.GetWritablePixels(0, 0, imagem.Width, imagem.Height); int range = _pixformat / 2; for (int w = 0; w < imagem.Width; w += _pixformat) { for (int h = 0; h < imagem.Height; h += _pixformat) { if (!(w + _pixformat <= imagem.Width && h + _pixformat <= imagem.Height)) continue;//olmazda olursa diye pixavg = new int[5, 3]; for (int x = 0; x < range; x++) { for (int y = 0; y < range; y++) { Color a = _totalpix.GetPixel(x + w + 0, h + 0 + y).ToColor().ToColor(); pixavg[0, 0] += a.R; pixavg[0, 1] += a.G; pixavg[0, 2] += a.B; Color b = _totalpix.GetPixel(x + w + range, h + y).ToColor().ToColor(); pixavg[1, 0] += b.R; pixavg[1, 1] += b.G; pixavg[1, 2] += b.B; Color c = _totalpix.GetPixel(x + w, h + range + y).ToColor().ToColor(); pixavg[2, 0] += c.R; pixavg[2, 1] += c.G; pixavg[2, 2] += c.B; Color d = _totalpix.GetPixel(x + w + range, h + range + y).ToColor().ToColor(); pixavg[3, 0] += d.R; pixavg[3, 1] += d.G; pixavg[3, 2] += d.B; } } //tümü için aynı toplanıyor pixavg[4, 0] = pixavg[0, 0] + pixavg[1, 0] + pixavg[2, 0] + pixavg[3, 0]; pixavg[4, 1] = pixavg[0, 1] + pixavg[1, 1] + pixavg[2, 1] + pixavg[3, 1]; pixavg[4, 2] = pixavg[0, 2] + pixavg[1, 2] + pixavg[2, 2] + pixavg[3, 2]; //---- int totalminiPix = (range * range); pixavg[0, 0] /= totalminiPix; pixavg[0, 1] /= totalminiPix; pixavg[0, 2] /= totalminiPix; pixavg[1, 0] /= totalminiPix; pixavg[1, 1] /= totalminiPix; pixavg[1, 2] /= totalminiPix; pixavg[2, 0] /= totalminiPix; pixavg[2, 1] /= totalminiPix; pixavg[2, 2] /= totalminiPix; pixavg[3, 0] /= totalminiPix; pixavg[3, 1] /= totalminiPix; pixavg[3, 2] /= totalminiPix; int totalPix = totalminiPix * 4; pixavg[4, 0] /= totalPix; pixavg[4, 1] /= totalPix; pixavg[4, 2] /= totalPix; sp0.Add(new ImgSquare(w, h, new List<QuardPixAvg>() { new QuardPixAvg(Color.FromArgb((pixavg[0, 0]), (pixavg[0, 1]), (pixavg[0, 2])), QuardBolum.SolUst), new QuardPixAvg (Color.FromArgb((pixavg[1, 0]), (pixavg[1, 1]), (pixavg[1, 2])), QuardBolum.SagUst), new QuardPixAvg(Color.FromArgb((pixavg[2, 0]), (pixavg[2, 1]), (pixavg[2, 2])), QuardBolum.SolAlt), new QuardPixAvg(Color.FromArgb((pixavg[3, 0]), (pixavg[3, 1]), (pixavg[3, 2])), QuardBolum.SagAlt), new QuardPixAvg(Color.FromArgb((pixavg[4, 0]), (pixavg[4, 1]), (pixavg[4, 2])), QuardBolum.TotalAvg) })); } } _totalpix = null; pixavg = null; ////////////////////////////// xml generate /////////////////////////////// #region xml oluşturma //XmlDocument doc = new XmlDocument(); //XmlElement root = doc.CreateElement("SquarePoints"); //root.SetAttribute("Count", sp0.Count.ToString()); //root.SetAttribute("PixFormat", _pixFor.ToString()); //foreach (ImgSquare item in sp0) //{ // XmlElement child = doc.CreateElement("SquarePoint"); // child.SetAttribute("WxH", item.ToString()); // child.SetAttribute("ColorAVG", item.SAvgArb.ToString()); // List<QuardPixAvg> lstQ = item.QuardAvg; // child.SetAttribute("SolUst", lstQ[0].QuardAvg.ToString()); // child.SetAttribute("SagUst", lstQ[1].QuardAvg.ToString()); // child.SetAttribute("SolAlt", lstQ[2].QuardAvg.ToString()); // child.SetAttribute("SagAlt", lstQ[3].QuardAvg.ToString()); // root.AppendChild(child); //} //doc.AppendChild(root); //if (!Directory.Exists(finf.Directory.FullName)) // Directory.CreateDirectory(finf.Directory.FullName); //doc.Save(path); //doc = null; #endregion } else { #region xml okuma //XmlDocument doc = new XmlDocument(); //doc.Load(path); //XmlNodeList root1 = doc.GetElementsByTagName("SquarePoints"); //XmlNode root2 = root1.Item(0); //foreach (XmlNode item in root2.ChildNodes) //{ // try // { // XmlAttribute at1 = item.Attributes[0]; // string[] point = at1.InnerText.Split('x'); // XmlAttribute at2 = item.Attributes[1]; // string ColorAvg = at2.InnerText; // List<QuardPixAvg> lQuard = new List<QuardPixAvg>(); // int SolUst = int.Parse(item.Attributes[2].InnerText); // int SagUst = int.Parse(item.Attributes[3].InnerText); // int SolAlt = int.Parse(item.Attributes[4].InnerText); // int SagAlt = int.Parse(item.Attributes[5].InnerText); // int Tumu = int.Parse(item.Attributes[1].InnerText); // lQuard.Add(new QuardPixAvg(Color.FromArgb(SolUst), QuardBolum.SolUst)); // lQuard.Add(new QuardPixAvg(Color.FromArgb(SagUst), QuardBolum.SagUst)); // lQuard.Add(new QuardPixAvg(Color.FromArgb(SolAlt), QuardBolum.SolAlt)); // lQuard.Add(new QuardPixAvg(Color.FromArgb(SagAlt), QuardBolum.SagAlt)); // lQuard.Add(new QuardPixAvg(Color.FromArgb(Tumu), QuardBolum.Tumu)); // sp0.Add(new ImgSquare(int.Parse(point[0]), int.Parse(point[1]), lQuard)); // } // catch (Exception e) // { // throw e; // } //} //root2 = null; //root1 = null; //doc = null; #endregion } //#--------------------------------- int opacity = 50; switch (_pixFor) { case PixFormat._null: sp0.Clear(); throw new Exception("Tanımsız format") { Source = "" }; break; case PixFormat._12x12: //12:36 sn opacity = 50; break; case PixFormat._20x20: //3:10 sn opacity = 50; break; case PixFormat._36x36: //0:50 sn opacity = 50; break; case PixFormat._48x48: //0:27 sn opacity = 50; break; case PixFormat._64x64: //0:15 sn opacity = 50; break; case PixFormat._94x94: //0:08 sn opacity = 50; break; } //string MiniPicturePath = Path.Combine(UserProperty.Data_Path, PixFormat._94x94.ToString()); //eski yükleme şekli string MiniPicturePath = Path.Combine(UserProperty.Data_InstagramPhotos); if (Directory.Exists(MiniPicturePath)) { Bitmap btm = imagem.ToBitmap(); using (Graphics gr1 = Graphics.FromImage(btm)) { //string[] list = Directory.GetFiles(MiniPicturePath, "*" + JPG); //string[] list = new string[0]; /////////////////////////////////////////////////////////////// string[] file = Directory.GetFiles(UserProperty.Current_User, UserProperty.UserXmlInfo); if (file.Count() != 1) throw new Exception("Geçersiz kullanıcı bilgileri"); XDocument doc = XDocument.Load(file[0]); XElement root = doc.Elements("_" + ImageProperty.GetUserName()).First(); XElement InstagramP = root.Elements("InstagramPhotos").FirstOrDefault(); XElement[] photos = (from p in InstagramP.Elements() where p.Attribute("useThis").Value.ToLower() == "true" select p).ToArray(); //list = new string[photos.Count()]; //for (int i = 0; i < photos.Count(); i++) // list[i] = Path.Combine(MiniPicturePath, photos[i].Value); /////////////////////////////////////////////////// if (photos.Count() == 0) throw new Exception("Bu Formata Uygun Resimler Bulunamadı") { Source = "" }; List<ImgSquare> spl2 = new List<ImgSquare>(); for (int i = 0; i < photos.Count(); i++) { if (File.Exists(Path.Combine(MiniPicturePath, Path.GetFileName(photos[i].Value)))) { using (MagickImage mini = new MagickImage(Path.Combine(MiniPicturePath, Path.GetFileName(photos[i].Value)))) { mini.Quality = 100; if (mini.Width != 94 && mini.Height != 94) mini.Resize((int)_pixFor, (int)_pixFor); //ImgSquare imgsq = new ImgSquare(mini.ToBitmap(), new List<QuardPixAvg>() { // new QuardPixAvg(Color.FromArgb(int.Parse(photos[i].Attribute("SolUst").Value)),QuardBolum.SagAlt), // new QuardPixAvg(Color.FromArgb(int.Parse(photos[i].Attribute("SagUst").Value)),QuardBolum.SagAlt), // new QuardPixAvg(Color.FromArgb(int.Parse(photos[i].Attribute("SolAlt").Value)),QuardBolum.SagAlt), // new QuardPixAvg(Color.FromArgb(int.Parse(photos[i].Attribute("SagAlt").Value)),QuardBolum.SagAlt), // new QuardPixAvg(Color.FromArgb(int.Parse(photos[i].Attribute("TotalAvg").Value)),QuardBolum.SagAlt) //}); spl2.Add(new ImgSquare(mini.ToBitmap())); mini.Dispose(); } } } photos = null; doc = null; root = null; InstagramP = null; List<ImgSquare> spl4 = new List<ImgSquare>(); spl4.AddRange(sp0); spl4.AddRange(spl2); spl2.Clear(); spl4 = spl4.OrderBy(p => p.GeneratedColorCode).ToList(); int qpiro_number = 432101; int qpiro_number2 = 0; int undefined = 0; for (int i = 0; i < sp0.Count; i++) { ImgSquare item = sp0[i]; //if (item.SAvgArb == 0 && item.IAvgRgb == 0) // continue; try { qpiro_number2 = 0; List<ImgSquare> snc = null; int cont = 0; do { snc = spl4.Where(p => (p.GeneratedColorCode - (qpiro_number + qpiro_number2) < item.GeneratedColorCode && p.GeneratedColorCode + (qpiro_number + qpiro_number2) > item.GeneratedColorCode) && p.isArea == false).ToList(); qpiro_number2 += 332101; cont++; } while (snc.Count == 0 && cont < 5); Rectangle rec = new Rectangle(item.W, item.H, (int)_pixFor, (int)_pixFor); if (snc.Count != 0) { int randi = random.Next(0, snc.Count); System.Drawing.Image img = SmartRotate(item, snc[randi]); snc.Clear();// img = Transparnt(img, opacity); gr1.DrawImage(img, rec); sp0.RemoveAt(i); i--; img.Dispose(); } else { using (MagickImage imgUndefined = new MagickImage(Path.Combine(UserProperty.Data_InstagramPhotos, "black.jpg"))) { System.Drawing.Image img = Transparnt(imgUndefined.ToBitmap(), opacity); gr1.DrawImage(img, rec); sp0.RemoveAt(i); i--; imgUndefined.Dispose(); } //int ss = item.GeneratedColorCode; //if (ss >= 11075440 && ss <= 15260100) //{ // cont = 0; // qpiro_number2 += 332101; // do // { // snc = spl4.Where(p => // (p.GeneratedColorCode - (qpiro_number + qpiro_number2) < item.GeneratedColorCode && // p.GeneratedColorCode + (qpiro_number + qpiro_number2) > item.GeneratedColorCode) && // p.isArea == false).ToList(); // qpiro_number2 += 332101; // cont++; // } // while (snc.Count == 0 && cont < 13); // if (snc.Count != 0) // { // Rectangle rec = new Rectangle(item.W, item.H, (int)_pixFor, (int)_pixFor); // int randi = random.Next(0, snc.Count); // System.Drawing.Image img = SmartRotate(item, snc[randi]); // snc.Clear();// // img = Transparnt(img, opacity); // gr1.DrawImage(img, rec); // sp0.RemoveAt(i); // i--; // } // else // { // } //} undefined++; } } catch (Exception ef) { throw ef; } } sp0.Clear(); spl4.Clear(); gr1.Dispose(); //workingBitmap1.Save(Path.Combine(UsrImageProc.SavedPhotos_Path, Path.GetFileName(inputBitmapPath))); //imglist.Add("lokale kaydettim"); Bitmap hd = btm.ArithmeticBlend(btm, ColorCalculationType.Add); imglist.Add(ImageToBase64(hd, System.Drawing.Imaging.ImageFormat.Jpeg)); imglist.Add(ImageToBase64(btm, System.Drawing.Imaging.ImageFormat.Jpeg)); btm.Dispose(); hd.Dispose(); imagem.Dispose(); } } else { throw new Exception("Bu Formata Uygun Resimler Bulunamadı2") { Source = "" }; } } } catch (Exception e) { throw e; } return imglist; }
private void DebugMaps() { MainForm.Log("Drawing debug bound images ...", MainForm.LogLevel.warning); MainForm.ProgressStartMarquee("Debug bound images ..."); DirectoryInfo debugDir = new DirectoryInfo(string.Format("{0}\\debug\\bound\\{1}", System.Windows.Forms.Application.StartupPath, zoneConfiguration.ZoneId)); if (!debugDir.Exists) debugDir.Create(); debugDir.GetFiles().ToList().ForEach(f => f.Delete()); int boundIndex = 0; foreach (List<PointF> allCoords in m_bounds) { using (MagickImage bound = new MagickImage(MagickColor.Transparent, zoneConfiguration.TargetMapSize, zoneConfiguration.TargetMapSize)) { List<Coordinate> coords = allCoords.Select(c => new Coordinate(zoneConfiguration.ZoneCoordinateToMapCoordinate(c.X), zoneConfiguration.ZoneCoordinateToMapCoordinate(c.Y))).ToList(); DrawablePolygon poly = new DrawablePolygon(coords); bound.FillColor = new MagickColor(0, 0, 0, 256 * 128); bound.Draw(poly); // Print Text for (int i = 0; i < coords.Count; i++) { double x, y; if (coords[i].X > zoneConfiguration.TargetMapSize / 2) x = coords[i].X - 15; else x = coords[i].X + 1; if (coords[i].Y < zoneConfiguration.TargetMapSize / 2) y = coords[i].Y + 15; else y = coords[i].Y - 1; bound.FontPointsize = 10.0; bound.FillColor = Color.Black; DrawableText text = new DrawableText(x, y, string.Format("{0} ({1}/{2})", i, zoneConfiguration.MapCoordinateToZoneCoordinate(coords[i].X), zoneConfiguration.MapCoordinateToZoneCoordinate(coords[i].Y))); bound.Draw(text); using (WritablePixelCollection pixels = bound.GetWritablePixels()) { int x2, y2; if (coords[i].X == zoneConfiguration.TargetMapSize) x2 = zoneConfiguration.TargetMapSize - 1; else x2 = (int)coords[i].X; if (coords[i].Y == zoneConfiguration.TargetMapSize) y2 = zoneConfiguration.TargetMapSize - 1; else y2 = (int)coords[i].Y; pixels.Set(x2, y2, new ushort[] { 0, 0, 65535, 0 }); } } //bound.Quality = 100; bound.Write(string.Format("{0}\\bound_{1}.png", debugDir.FullName, boundIndex)); boundIndex++; } } MainForm.ProgressReset(); }
public void Draw(MagickImage map) { MainForm.ProgressStart("Rendering water ..."); using (PixelCollection heightmapPixels = zoneConfiguration.Heightmap.HeightmapScaled.GetReadOnlyPixels()) { using (MagickImage water = new MagickImage(MagickColor.Transparent, zoneConfiguration.TargetMapSize, zoneConfiguration.TargetMapSize)) { int progressCounter = 0; foreach (WaterConfiguration river in m_waterAreas) { MainForm.Log(river.Name + "...", MainForm.LogLevel.notice); MagickColor fillColor; if (m_useClientColors) fillColor = river.Color; else fillColor = m_waterColor; //water.FillColor = fillColor; // Get the river coordinates and scale them to the targets size List<Coordinate> riverCoordinates = river.GetCoordinates().Select(c => new Coordinate(c.X * zoneConfiguration.MapScale, c.Y * zoneConfiguration.MapScale)).ToList(); // Texture using (MagickImage texture = new MagickImage((river.Type.ToLower() == "lava") ? GetLavaTexture() : GetWateryTexture())) { using (MagickImage pattern = new MagickImage(fillColor, texture.Width, texture.Height)) { texture.Composite(pattern, 0, 0, CompositeOperator.DstIn); texture.Composite(pattern, 0, 0, CompositeOperator.ColorDodge); water.FillPattern = texture; DrawablePolygon poly = new DrawablePolygon(riverCoordinates); water.Draw(poly); } } // get the min/max and just process them int minX = Convert.ToInt32(riverCoordinates.Min(m => m.X)) - 10; int maxX = Convert.ToInt32(riverCoordinates.Max(m => m.X)) + 10; int minY = Convert.ToInt32(riverCoordinates.Min(m => m.Y)) - 10; int maxY = Convert.ToInt32(riverCoordinates.Max(m => m.Y)) + 10; using (WritablePixelCollection riverPixelCollection = water.GetWritablePixels()) { for (int x = minX; x < maxX; x++) { if (x < 0) continue; if (x >= zoneConfiguration.TargetMapSize) continue; for (int y = minY; y < maxY; y++) { if (y < 0) continue; if (y >= zoneConfiguration.TargetMapSize) continue; ushort pixelHeight = heightmapPixels.GetPixel(x, y).GetChannel(0); if (pixelHeight > river.Height) { Pixel newPixel = new Pixel(x, y, new ushort[] { 0, 0, 0, ushort.MinValue }); riverPixelCollection.Set(newPixel); } } } } if (debug) { DebugRiver(progressCounter, river, riverCoordinates); } int percent = 100 * progressCounter / m_waterAreas.Count(); MainForm.ProgressUpdate(percent); progressCounter++; } MainForm.ProgressStartMarquee("Merging..."); if (WaterTransparency != 0) { water.Alpha(AlphaOption.Set); double divideValue = 100.0 / (100.0 - WaterTransparency); water.Evaluate(Channels.Alpha, EvaluateOperator.Divide, divideValue); } water.Blur(); map.Composite(water, 0, 0, CompositeOperator.SrcOver); } } MainForm.ProgressReset(); }
private void GenerateHeightmap() { if(this.heightmapGenerated) return; MainForm.ProgressStart("Processing heightmap ..."); using (MagickImage offsetmap = zoneConfiguration.GetOffsetMap()) { using (MagickImage terrainmap = zoneConfiguration.GetTerrainMap()) { m_heightmap = new MagickImage(Color.Black, offsetmap.Width, offsetmap.Height); using (WritablePixelCollection heightmapPixels = m_heightmap.GetWritablePixels()) { PixelCollection terrainPixels = terrainmap.GetReadOnlyPixels(); PixelCollection offsetPixels = offsetmap.GetReadOnlyPixels(); for (int x = 0; x < offsetmap.Width; x++) { for (int y = 0; y < offsetmap.Height; y++) { ushort terrainPixelValue = (ushort)(terrainPixels[x, y].GetChannel(0) / 256); ushort offsetPixelValue = (ushort)(offsetPixels[x, y].GetChannel(0) / 256); ushort heightmapPixelValue = (ushort)(terrainPixelValue * m_terrainfactor + offsetPixelValue * m_offsetfactor); heightmapPixels.Set(x, y, new ushort[] { heightmapPixelValue, heightmapPixelValue, heightmapPixelValue }); } int percent = 100 * x / offsetmap.Width; MainForm.ProgressUpdate(percent); } heightmapPixels.Write(); } MainForm.ProgressStartMarquee("Merging ..."); m_heightmap.Quality = 100; m_heightmap.Write(m_heightmapFile.FullName); // Scale to target size m_heightmapScaled = new MagickImage(m_heightmap); m_heightmapScaled.Resize(zoneConfiguration.TargetMapSize, zoneConfiguration.TargetMapSize); } } this.heightmapGenerated = true; MainForm.ProgressReset(); }
public void Test_GetValue() { using (MagickImage image = new MagickImage(Color.Red, 5, 10)) { using (WritablePixelCollection pixels = image.GetWritablePixels()) { Test_PixelColor(pixels, Color.Red); } } }
public void Test_Dimensions() { using (MagickImage image = new MagickImage(Color.Red, 5, 10)) { using (WritablePixelCollection pixels = image.GetWritablePixels()) { Assert.AreEqual(5, pixels.Width); Assert.AreEqual(10, pixels.Height); Assert.AreEqual(3, pixels.Channels); Assert.AreEqual(5 * 10 * pixels.Channels, pixels.GetValues().Length); } } }
public void Test_Write() { using (MagickImage image = new MagickImage(Color.Red, 10, 1)) { using (WritablePixelCollection pixels = image.GetWritablePixels()) { byte[] bytes = new byte[10 * pixels.Channels]; for (int i = 0; i < bytes.Length; i++) bytes[i] = 0; pixels.Set(bytes); pixels.Write(); } TestPixels(image, new MagickColor(0, 0, 0)); using (WritablePixelCollection pixels = image.GetWritablePixels()) { foreach (Pixel pixel in pixels) { pixel.SetChannel(2, Quantum.Max); } pixels.Write(); } TestPixels(image, Color.Blue); } }
public void Test_Set() { using (MagickImage image = new MagickImage(Color.Red, 5, 10)) { using (WritablePixelCollection pixels = image.GetWritablePixels()) { ExceptionAssert.Throws<ArgumentNullException>(delegate () { pixels.Set((QuantumType[])null); }); ExceptionAssert.Throws<ArgumentNullException>(delegate () { pixels.Set((Pixel)null); }); ExceptionAssert.Throws<ArgumentNullException>(delegate () { pixels.Set((Pixel[])null); }); Assert.AreEqual(3, pixels.Channels); Test_Set(pixels, new QuantumType[] { }); Test_Set(pixels, new QuantumType[] { 0 }); Test_Set(pixels, new QuantumType[] { 0, 0 }); pixels.Set(new QuantumType[] { 0, 0, 0 }); Test_PixelColor(pixels, Color.Black); pixels.Write(); } using (PixelCollection pixels = image.GetReadOnlyPixels()) { Test_PixelColor(pixels, Color.Black); } using (WritablePixelCollection pixels = image.GetWritablePixels()) { pixels.Set(new uint[] { 4294967295, 0, 0 }); Test_PixelColor(pixels, Color.Red); pixels.Set(new ushort[] { 0, 0, 65535 }); Test_PixelColor(pixels, Color.Blue); pixels.Set(new byte[] { 0, 255, 0 }); Test_PixelColor(pixels, Color.Lime); } using (WritablePixelCollection pixels = image.GetWritablePixels()) { for (int x = 0; x < pixels.Width; x++) { for (int y = 0; y < pixels.Height; y++) { pixels.Set(x, y, new QuantumType[] { 0, 0, 0 }); } } } } }
public static NewImagePart PointGenerator(string UserName, byte[] ImagePart, int _x, int _y, int width, int height, int PxFormat) { try { Rectangle recti = new Rectangle(_x, _y, width, height); PixFormat _pixFor = (PixFormat)StringToEnum(typeof(PixFormat), Convert.ToString("_" + PxFormat.ToString() + "x" + PxFormat.ToString())); qprPath resources = new qprPath(UserName); if (_pixFor == PixFormat._null) throw new Exception("Format null olamaz") { Source = "" }; //GC.Collect(); //Bitmap bmp; //using (var ms = new MemoryStream(ImagePart)) //{ // bmp = new Bitmap(ms); //} using (MagickImage imagem = new MagickImage(ImagePart)) { imagem.Quality = 100; imagem.Blur(5, 5); List<ImgSquare> sp0 = new List<ImgSquare>(); if (true) { int[,] pixavg = new int[5, 3]; int _pixformat = (int)_pixFor; WritablePixelCollection _totalpix = imagem.GetWritablePixels(0, 0, imagem.Width, imagem.Height); int range = _pixformat / 2; for (int w = 0; w < imagem.Width; w += _pixformat) { for (int h = 0; h < imagem.Height; h += _pixformat) { if (!(w + _pixformat <= imagem.Width && h + _pixformat <= imagem.Height)) continue;//olmazda olursa diye pixavg = new int[5, 3]; for (int x = 0; x < range; x++) { for (int y = 0; y < range; y++) { Color a = _totalpix.GetPixel(x + w + 0, h + 0 + y).ToColor().ToColor(); pixavg[0, 0] += a.R; pixavg[0, 1] += a.G; pixavg[0, 2] += a.B; Color b = _totalpix.GetPixel(x + w + range, h + y).ToColor().ToColor(); pixavg[1, 0] += b.R; pixavg[1, 1] += b.G; pixavg[1, 2] += b.B; Color c = _totalpix.GetPixel(x + w, h + range + y).ToColor().ToColor(); pixavg[2, 0] += c.R; pixavg[2, 1] += c.G; pixavg[2, 2] += c.B; Color d = _totalpix.GetPixel(x + w + range, h + range + y).ToColor().ToColor(); pixavg[3, 0] += d.R; pixavg[3, 1] += d.G; pixavg[3, 2] += d.B; } } //tümü için aynı toplanıyor pixavg[4, 0] = pixavg[0, 0] + pixavg[1, 0] + pixavg[2, 0] + pixavg[3, 0]; pixavg[4, 1] = pixavg[0, 1] + pixavg[1, 1] + pixavg[2, 1] + pixavg[3, 1]; pixavg[4, 2] = pixavg[0, 2] + pixavg[1, 2] + pixavg[2, 2] + pixavg[3, 2]; //---- int totalminiPix = (range * range); pixavg[0, 0] /= totalminiPix; pixavg[0, 1] /= totalminiPix; pixavg[0, 2] /= totalminiPix; pixavg[1, 0] /= totalminiPix; pixavg[1, 1] /= totalminiPix; pixavg[1, 2] /= totalminiPix; pixavg[2, 0] /= totalminiPix; pixavg[2, 1] /= totalminiPix; pixavg[2, 2] /= totalminiPix; pixavg[3, 0] /= totalminiPix; pixavg[3, 1] /= totalminiPix; pixavg[3, 2] /= totalminiPix; int totalPix = totalminiPix * 4; pixavg[4, 0] /= totalPix; pixavg[4, 1] /= totalPix; pixavg[4, 2] /= totalPix; sp0.Add(new ImgSquare(w, h, new List<QuardPixAvg>() { new QuardPixAvg(Color.FromArgb((pixavg[0, 0]), (pixavg[0, 1]), (pixavg[0, 2])), QuardBolum.SolUst), new QuardPixAvg (Color.FromArgb((pixavg[1, 0]), (pixavg[1, 1]), (pixavg[1, 2])), QuardBolum.SagUst), new QuardPixAvg(Color.FromArgb((pixavg[2, 0]), (pixavg[2, 1]), (pixavg[2, 2])), QuardBolum.SolAlt), new QuardPixAvg(Color.FromArgb((pixavg[3, 0]), (pixavg[3, 1]), (pixavg[3, 2])), QuardBolum.SagAlt), new QuardPixAvg(Color.FromArgb((pixavg[4, 0]), (pixavg[4, 1]), (pixavg[4, 2])), QuardBolum.TotalAvg) })); } } _totalpix = null; pixavg = null; ////////////////////////////// xml generate /////////////////////////////// #region xml oluşturma //XmlDocument doc = new XmlDocument(); //XmlElement root = doc.CreateElement("SquarePoints"); //root.SetAttribute("Count", sp0.Count.ToString()); //root.SetAttribute("PixFormat", _pixFor.ToString()); //foreach (ImgSquare item in sp0) //{ // XmlElement child = doc.CreateElement("SquarePoint"); // child.SetAttribute("WxH", item.ToString()); // child.SetAttribute("ColorAVG", item.SAvgArb.ToString()); // List<QuardPixAvg> lstQ = item.QuardAvg; // child.SetAttribute("SolUst", lstQ[0].QuardAvg.ToString()); // child.SetAttribute("SagUst", lstQ[1].QuardAvg.ToString()); // child.SetAttribute("SolAlt", lstQ[2].QuardAvg.ToString()); // child.SetAttribute("SagAlt", lstQ[3].QuardAvg.ToString()); // root.AppendChild(child); //} //doc.AppendChild(root); //if (!Directory.Exists(finf.Directory.FullName)) // Directory.CreateDirectory(finf.Directory.FullName); //doc.Save(path); //doc = null; #endregion xml oluşturma } else { #region xml okuma //XmlDocument doc = new XmlDocument(); //doc.Load(path); //XmlNodeList root1 = doc.GetElementsByTagName("SquarePoints"); //XmlNode root2 = root1.Item(0); //foreach (XmlNode item in root2.ChildNodes) //{ // try // { // XmlAttribute at1 = item.Attributes[0]; // string[] point = at1.InnerText.Split('x'); // XmlAttribute at2 = item.Attributes[1]; // string ColorAvg = at2.InnerText; // List<QuardPixAvg> lQuard = new List<QuardPixAvg>(); // int SolUst = int.Parse(item.Attributes[2].InnerText); // int SagUst = int.Parse(item.Attributes[3].InnerText); // int SolAlt = int.Parse(item.Attributes[4].InnerText); // int SagAlt = int.Parse(item.Attributes[5].InnerText); // int Tumu = int.Parse(item.Attributes[1].InnerText); // lQuard.Add(new QuardPixAvg(Color.FromArgb(SolUst), QuardBolum.SolUst)); // lQuard.Add(new QuardPixAvg(Color.FromArgb(SagUst), QuardBolum.SagUst)); // lQuard.Add(new QuardPixAvg(Color.FromArgb(SolAlt), QuardBolum.SolAlt)); // lQuard.Add(new QuardPixAvg(Color.FromArgb(SagAlt), QuardBolum.SagAlt)); // lQuard.Add(new QuardPixAvg(Color.FromArgb(Tumu), QuardBolum.Tumu)); // sp0.Add(new ImgSquare(int.Parse(point[0]), int.Parse(point[1]), lQuard)); // } // catch (Exception e) // { // throw e; // } //} //root2 = null; //root1 = null; //doc = null; #endregion xml okuma } //#--------------------------------- int opacity = 50; switch (_pixFor) { case PixFormat._null: sp0.Clear(); throw new Exception("Tanımsız format") { Source = "" }; break; case PixFormat._12x12: opacity = 50; break; case PixFormat._20x20: opacity = 50; break; case PixFormat._36x36: opacity = 50; break; case PixFormat._48x48: opacity = 50; break; case PixFormat._64x64: opacity = 50; break; case PixFormat._94x94: opacity = 50; break; } string MiniPicturePath = Path.Combine(resources.Data_InstagramPhotos); if (Directory.Exists(MiniPicturePath)) { Bitmap btm = imagem.ToBitmap(); using (Graphics gr1 = Graphics.FromImage(btm)) { /////////////////////////////////////////////////////////////// string[] file = Directory.GetFiles(resources.Current_User, resources.UserXmlInfo); if (file.Count() != 1) throw new Exception("Geçersiz kullanıcı bilgileri"); XDocument doc = XDocument.Load(file[0]); XElement root = doc.Elements("_" + resources.UserName).First(); XElement InstagramP = root.Elements("InstagramPhotos").FirstOrDefault(); XElement[] photos = (from p in InstagramP.Elements() where p.Attribute("useThis").Value.ToLower() == "true" select p).ToArray(); /////////////////////////////////////////////////// if (photos.Count() == 0) throw new Exception("Bu Formata Uygun Resimler Bulunamadı") { Source = "" }; List<ImgSquare> spl2 = new List<ImgSquare>(); for (int i = 0; i < photos.Count(); i++) { if (File.Exists(Path.Combine(MiniPicturePath, Path.GetFileName(photos[i].Value)))) { using (MagickImage mini = new MagickImage(Path.Combine(MiniPicturePath, Path.GetFileName(photos[i].Value)))) { mini.Quality = 100; if (mini.Width != 94 && mini.Height != 94) mini.Resize((int)_pixFor, (int)_pixFor); spl2.Add(new ImgSquare(mini.ToBitmap())); mini.Dispose(); } } } photos = null; doc = null; root = null; InstagramP = null; List<ImgSquare> spl4 = new List<ImgSquare>(); spl4.AddRange(sp0); spl4.AddRange(spl2); spl2.Clear(); spl4 = spl4.OrderBy(p => p.GeneratedColorCode).ToList(); int qpiro_number = 432101; int qpiro_number2 = 0; int undefined = 0; for (int i = 0; i < sp0.Count; i++) { ImgSquare item = sp0[i]; try { qpiro_number2 = 0; List<ImgSquare> snc = null; int cont = 0; do { snc = spl4.Where(p => (p.GeneratedColorCode - (qpiro_number + qpiro_number2) < item.GeneratedColorCode && p.GeneratedColorCode + (qpiro_number + qpiro_number2) > item.GeneratedColorCode) && p.isArea == false).ToList(); qpiro_number2 += 332101; cont++; } while (snc.Count == 0 && cont < 5); Rectangle rec = new Rectangle(item.W, item.H, (int)_pixFor, (int)_pixFor); if (snc.Count != 0) { int randi = random.Next(0, snc.Count); System.Drawing.Image img = SmartRotate(item, snc[randi]); snc.Clear();// img = Transparnt(img, opacity); gr1.DrawImage(img, rec); sp0.RemoveAt(i); i--; img.Dispose(); } else { cont = 0; qpiro_number2 = (332101 * 5); do { snc = spl4.Where(p => (p.GeneratedColorCode - (qpiro_number + qpiro_number2) < item.GeneratedColorCode && p.GeneratedColorCode + (qpiro_number + qpiro_number2) > item.GeneratedColorCode) && p.isArea == false).ToList(); qpiro_number2 += 332101; cont++; } while (snc.Count == 0 && cont < 11); if (snc.Count > 0) { int randi = random.Next(0, snc.Count); System.Drawing.Image img = SmartRotate(item, snc[randi]); snc.Clear(); Bitmap bt0 = new Bitmap(rec.Width, rec.Height); Graphics g = Graphics.FromImage(bt0); g.Clear(Color.FromArgb(item.GeneratedColorCode)); if (opacity != 100) { img = Transparnt(img, opacity + 20); } g.DrawImage(img, new Rectangle(0, 0, rec.Width, rec.Height)); g.Dispose(); gr1.DrawImage(bt0, rec); sp0.RemoveAt(i); i--; img.Dispose(); } undefined++; } } catch (Exception ef) { throw ef; } } sp0.Clear(); spl4.Clear(); gr1.Dispose(); imagem.Dispose(); return new NewImagePart() { newImage = new MagickImage(btm).ToByteArray(), ImagePartInfo = recti.ToString() }; } } else { throw new Exception("Bu Formata Uygun Resimler Bulunamadı2") { Source = "" }; } } } catch (Exception e) { throw e; } return null; }