コード例 #1
0
 public void Dispose()
 {
     ReflectionTexture.Clear();
     BgTexture.Clear();
     SkyTexture.Clear();
     Wallpaper.Clear();
 }
コード例 #2
0
        public void HandleWallpaper(ViewInfo view, bool scaleToFit)
        {
            var file = Rhino.Render.Utilities.FindFile(RhinoDoc.ActiveDoc, view.WallpaperFilename);

            bool modifiedWallpaper = false;

            modifiedWallpaper |= m_old_hidden != view.WallpaperHidden | m_old_grayscale != view.ShowWallpaperInGrayScale;
            modifiedWallpaper |= m_old_scaletofit != scaleToFit;
            modifiedWallpaper |= !m_old_wallpaper.Equals(file);
            Modified          |= modifiedWallpaper;

            if (string.IsNullOrEmpty(file) || !File.Exists(file))
            {
                RcCore.OutputDebugString($"{file} not found, clearing and returning\n");
                Wallpaper.Clear();
                WallpaperSolid = Color1;
                return;
            }

            if (!modifiedWallpaper)
            {
                return;
            }

            m_old_hidden     = view.WallpaperHidden;
            m_old_grayscale  = view.ShowWallpaperInGrayScale;
            m_old_scaletofit = scaleToFit;
            m_old_wallpaper  = file ?? "";
            var crc = Rhino.RhinoMath.CRC32(27, System.Text.Encoding.UTF8.GetBytes(m_old_wallpaper));

            RcCore.OutputDebugString($"Handling wallpaper, reading {file}\n");
            try
            {
                int near, far;
                var screenport = view.Viewport.GetScreenPort(out near, out far);
                var bottom     = screenport.Bottom;
                var top        = screenport.Top;
                var left       = screenport.Left;
                var right      = screenport.Right;

                // color matrix used for conversion to gray scale
                ColorMatrix cmgray = new ColorMatrix(
                    new[] {
                    new[] { 0.3f, 0.3f, 0.3f, 0.0f, 0.0f },
                    new[] { .59f, .59f, .59f, 0.0f, 0.0f },
                    new[] { .11f, .11f, .11f, 0.0f, 0.0f },
                    new[] { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f },
                    new[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }
                }
                    );
                ColorMatrix cmcolor = new ColorMatrix(
                    new[] {
                    new[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
                    new[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
                    new[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
                    new[] { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f },
                    new[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }
                }
                    );
                ImageAttributes attr = new ImageAttributes();
                attr.SetColorMatrix(view.ShowWallpaperInGrayScale ? cmgray : cmcolor);

                var    w           = Math.Abs(right - left);
                var    h           = Math.Abs(bottom - top);
                var    viewport_ar = (float)w / h;
                Bitmap bm          = new Bitmap(file);
                var    image_ar    = (float)bm.Width / bm.Height;

                int nw = 0;
                int nh = 0;
                int x  = 0;
                int y  = 0;
                if (image_ar > viewport_ar)
                {
                    x  = 0;
                    nw = w;
                    nh = (int)(w / image_ar);
                    y  = (int)(h * 0.5 - nh * 0.5);
                }
                else
                {
                    y  = 0;
                    nh = h;
                    nw = (int)(h * image_ar);
                    x  = (int)(w * 0.5 - nw * 0.5);
                }

                Bitmap newBitmap = new Bitmap(w, h);

                var col = Color.Aqua;
                if (Color1 != Color.Empty)
                {
                    col = Color1;
                }
                var brush  = new SolidBrush(col);
                var p      = new Point(x, y);
                var bmsize = new Size(nw, nh);
                if (scaleToFit)
                {
                    bmsize = new Size(w, h);
                    p      = new Point(0, 0);
                }
                using (Graphics g = Graphics.FromImage(newBitmap))
                {
                    g.FillRectangle(brush, new Rectangle(Point.Empty, newBitmap.Size));
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    if (!view.WallpaperHidden)
                    {
                        g.DrawImage(bm, new Rectangle(p, bmsize), 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel, attr);
                    }
                }
                var wallpaperbm = BitmapConverter.ReadByteBitmapFromBitmap(crc, newBitmap.Size.Width, newBitmap.Size.Height, newBitmap);
                wallpaperbm.ApplyGamma(Gamma);
                Wallpaper.TexByte = wallpaperbm.Data;
                if (RcCore.It.EngineSettings.SaveDebugImages)
                {
                    wallpaperbm.SaveBitmaps();
                }
                Wallpaper.TexWidth  = newBitmap.Width;
                Wallpaper.TexHeight = newBitmap.Height;
                Wallpaper.Name      =
                    $"{file}_{newBitmap.Width}x{newBitmap.Height}_{view.WallpaperHidden}_{view.ShowWallpaperInGrayScale}_{scaleToFit}_{id}";
            }
            catch (Exception e)
            {
                RcCore.OutputDebugString($"wallpaper failure: {e.Message}.\n");
                Wallpaper.Clear();
            }
        }