public static Point RandomTrimmingCenter(Element element, Bitmap img) { double angle = element.angle * Math.PI / 180.0; Point ret = new Point(); Size ellipseSize = new Size((int)(Math.Abs(element.xr * Math.Cos(angle)) + Math.Abs(element.yr * Math.Sin(angle))), (int)(Math.Abs(element.xr * Math.Sin(angle)) + Math.Abs(element.yr * Math.Cos(angle)))); Bitmap ellipseBound = new Bitmap(ellipseSize.Width, ellipseSize.Height); Graphics eg = Graphics.FromImage(ellipseBound); eg.Clear(backColor); eg.InterpolationMode = InterpolationMode.HighQualityBicubic; eg.PixelOffsetMode = PixelOffsetMode.HighQuality; eg.TranslateTransform(ellipseSize.Width / 2, ellipseSize.Height / 2); eg.RotateTransform(element.angle); eg.FillEllipse(Brushes.White, new Rectangle(-element.xr / 2, -element.yr / 2, element.xr, element.yr)); eg.Dispose(); //ellipseBound.Save("ellipse.png"); BitmapData data = ellipseBound.LockBits( new Rectangle(0, 0, ellipseSize.Width, ellipseSize.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); byte[] buf = new byte[ellipseSize.Width * ellipseSize.Height * 4]; Marshal.Copy(data.Scan0, buf, 0, buf.Length); ellipseBound.UnlockBits(data); List<int> points = new List<int>(); for (int i = 0; i < ellipseSize.Width * ellipseSize.Height; i++) { if (255 <= buf[i * 4 + 3]) { points.Add(i); } } int[] pointArray = points.ToArray(); while (true) { int index = pointArray[MyRandom.Next((uint)pointArray.Length)]; Point p = new Point((index % ellipseSize.Width - ellipseSize.Width / 2), (index / ellipseSize.Width - ellipseSize.Height / 2)); ret.X = p.X + element.pos.X; ret.Y = p.Y + element.pos.Y; try { if (img.GetPixel(ret.X, ret.Y).A >= 255) { break; } } catch (Exception) { Console.WriteLine("GetPixelメソッド参照範囲外"); continue; } } return ret; }
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (img != null) img.Dispose(); img = new Bitmap(imgPath + comboBox1.Text + ".png"); pictureBox1.Image = img; label10.Text = (comboBox1.SelectedIndex + 1) + "/" + comboBox1.Items.Count; elements = new Element[comboBox5.Items.Count - 1]; for (int i = 0; i < elements.Length; i++) { elements[i] = new Element(); } LoadSetting(); SetValue(); pictureBox1.Refresh(); }
public static Bitmap TrimmingBitmap(Element element, Bitmap origin, Size size) { Bitmap ret = new Bitmap(size.Width, size.Height); Graphics g = Graphics.FromImage(ret), gr = Graphics.FromImage(origin); Point center = RandomTrimmingCenter(element, origin); centerLast = center; //gr.InterpolationMode = InterpolationMode.HighQualityBicubic; //gr.PixelOffsetMode = PixelOffsetMode.HighQuality; //gr.TranslateTransform(center.X, center.Y); //gr.RotateTransform(element.angle); //gr.FillEllipse(Brushes.Red, -5, -5, 10, 10); //gr.Dispose(); g.Clear(backColor); g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.DrawImage(origin, new Rectangle(0, 0, size.Width, size.Height), new Rectangle(center.X - size.Width / 2, center.Y - size.Height / 2, size.Width, size.Height), GraphicsUnit.Pixel); g.Dispose(); return ret; }
private void LoadSetting(string file, int elementNum) { string path = settingPath + file + ".txt"; using (StreamReader sr = new StreamReader(path, Encoding.GetEncoding("Shift_JIS"))) { identity = new Identity(); identity.name = sr.ReadLine(); identity.kind = int.Parse(sr.ReadLine()); identity.update = int.Parse(sr.ReadLine()); identity.damage = int.Parse(sr.ReadLine()); for (int i = 0; i < elementNum; i++) { sr.ReadLine(); sr.ReadLine(); sr.ReadLine(); sr.ReadLine(); sr.ReadLine(); } element = new Element(); element.pos.X = int.Parse(sr.ReadLine()); element.pos.Y = int.Parse(sr.ReadLine()); element.xr = int.Parse(sr.ReadLine()); element.yr = int.Parse(sr.ReadLine()); element.angle = int.Parse(sr.ReadLine()); } }