コード例 #1
0
 /// <summary>
 /// Create a new cache slaved to a specific projection.
 /// </summary>
 public GdiCache(BitmapProjection projection)
 {
     Projection = projection ?? throw new ArgumentNullException(nameof(projection));
 }
コード例 #2
0
        protected override void SolveInstance(IGH_DataAccess access)
        {
            List <IGdiGoo> shapes     = new List <IGdiGoo>();
            Rectangle3d    boundary   = Rectangle3d.Unset;
            double         factor     = 1.0;
            Color          background = Color.Transparent;
            bool           antialias  = true;
            string         file       = null;

            access.GetDataList(0, shapes);
            if (!access.GetData(1, ref boundary))
            {
                return;
            }
            if (!access.GetData(2, ref factor))
            {
                return;
            }
            if (!access.GetData(3, ref background))
            {
                return;
            }
            if (!access.GetData(4, ref antialias))
            {
                return;
            }
            if (!access.GetData(5, ref file))
            {
                return;
            }

            if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(file)))
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Output folder does not exist.");
                return;
            }

            if (!boundary.IsValid)
            {
                return;
            }
            boundary.MakeIncreasing();
            if (boundary.Area < 1e-12)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Bitmap boundary is too small.");
                return;
            }
            if (factor < 1e-12)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Scaling factor is too small.");
                return;
            }

            int w = (int)(boundary.Width * factor);
            int h = (int)(boundary.Height * factor);

            if (w <= 0 || h <= 0)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Bitmap boundary is too narrow.");
                return;
            }

            BitmapProjection projection = new BitmapProjection(boundary, w, h);

            Bitmap   image    = new Bitmap(w, h, PixelFormat.Format32bppArgb);
            Graphics graphics = Graphics.FromImage(image);

            graphics.Clear(background);
            graphics.SmoothingMode     = antialias ? SmoothingMode.HighQuality : SmoothingMode.None;
            graphics.TextRenderingHint = antialias ? TextRenderingHint.AntiAliasGridFit : TextRenderingHint.SingleBitPerPixelGridFit;

            GdiCache cache = new GdiCache(projection);

            foreach (IGdiGoo shape in shapes)
            {
                shape.DrawShape(graphics, cache);
            }
            cache.Clear();
            graphics.Dispose();

            if (!string.IsNullOrWhiteSpace(file))
            {
                ImageFormat format;
                switch (System.IO.Path.GetExtension(file).ToLowerInvariant())
                {
                case ".png":
                    format = ImageFormat.Png;
                    break;

                case ".jpg":
                case ".jpeg":
                    format = ImageFormat.Jpeg;
                    break;

                case ".bmp":
                    format = ImageFormat.Bmp;
                    break;

                case ".tif":
                case ".tiff":
                    format = ImageFormat.Tiff;
                    break;

                default:
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Image extension is unknown, assuming PNG.");
                    format = ImageFormat.Png;
                    break;
                }

                try
                {
                    image.Save(file, format);
                }
                catch (Exception e)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Image could not be saved to disk: " + e.Message);
                }
            }

            image.Dispose();
        }