public static ConfigFile Load(string filename) { var result = new ConfigFile(); var list = result.SizeList; using (var sr = new System.IO.StreamReader(filename)) { while (!sr.EndOfStream) { string line = sr.ReadLine(); if (line.Trim().StartsWith("#")) { continue; } string[] parts = line.Split(separator, StringSplitOptions.RemoveEmptyEntries); if (parts.Length < 4) { continue; } float w = float.Parse(parts[0]); float h = float.Parse(parts[1]); eUnit u = (eUnit)Enum.Parse(typeof(eUnit), parts[2]); string n = parts[3]; var item = new CanvasSize() { name = n, width = w, height = h, unit = u }; list.Add(item); } } return(result); }
public static void DumpInitialConfigFile(string filename) { using (var sw = new System.IO.StreamWriter(filename, false)) { { sw.WriteLine("# A4 paper."); var size = new CanvasSize() { name = "A4", width = 8.267f, height = 11.692f, unit = eUnit.inch }; sw.WriteLine($"{size.width}, {size.height}, {size.unit}, {size.name}"); } { sw.WriteLine("# HuaWei M6."); var size = new CanvasSize() { name = "HuaWei M6", width = 5.724f, height = 9.1584f, unit = eUnit.inch }; sw.WriteLine($"{size.width}, {size.height}, {size.unit}, {size.name}"); } } }
private void UpdateImageInfo() { if (this.sourceImage == null) { return; } float xDpi = this.sourceImage.HorizontalResolution; float yDpi = this.sourceImage.VerticalResolution; float width = this.sourceImage.Width; float height = this.sourceImage.Height; CanvasSize size; if (this.cmbSolution.SelectedIndex >= 0) { size = (CanvasSize)this.cmbSolution.SelectedItem; } else { size = new CanvasSize() { width = width, height = height, unit = eUnit.inch }; } bool 纵向 = this.rdoVertical.Checked; // A4: 8.267 x 11.692 inches float hTarget = yDpi * (纵向 ? size.width : size.height); float vTarget = xDpi * (纵向 ? size.height : size.width); int pH = (int)Math.Ceiling(width / hTarget); int pV = (int)Math.Ceiling(height / vTarget); var builder = new StringBuilder(); //builder.AppendFormat($"fullname:{this.filename}"); //builder.AppendLine(); builder.AppendFormat($"DPI:{xDpi}, {yDpi}"); //builder.AppendLine(); builder.AppendFormat($", Size:{width}, {height}"); builder.AppendLine(); builder.AppendFormat($"=>:{pH}*{pV} parts."); builder.AppendLine(); this.txtImageInfo.Text = builder.ToString(); { Image old = this.imgSource.Image; this.imgSource.Image = null; if (old != null) { old.Dispose(); } } var newImage = new Bitmap(this.sourceImage); float cursorX = 0; float cursorY = 0; int indexX = 0; int indexY = 0; using (var g = Graphics.FromImage(newImage)) { while (cursorY <= height) { while (cursorX <= width) { g.DrawRectangle(Pens.Red, cursorX, cursorY, hTarget, vTarget); indexX++; cursorX = cursorX + hTarget; } indexY++; cursorX = 0; cursorY = cursorY + vTarget; } } this.imgSource.Image = newImage; }