예제 #1
0
        public void SaveImageToFile(string path)
        {
            string        extension = Path.GetExtension(path).ToLower();
            DrawingVisual dv        = new DrawingVisual();

            using (DrawingContext dc = dv.RenderOpen())
            {
                if (extension == ".bmp" || extension == "jpg")
                {
                    dc.DrawRectangle(Brushes.White, null, new Rect(0, 0, Math.Ceiling(Width + Location.X * 2), Math.Ceiling(Width + Location.Y * 2)));
                }
                ActiveChild.DrawEquation(dc);
            }
            RenderTargetBitmap bitmap = new RenderTargetBitmap((int)(Math.Ceiling(Width + Location.X * 2)), (int)(Math.Ceiling(Height + Location.Y * 2)), 96, 96, PixelFormats.Default);

            bitmap.Render(dv);
            BitmapEncoder encoder = null;

            switch (extension)
            {
            case ".jpg":
                encoder = new JpegBitmapEncoder();
                break;

            case ".gif":
                encoder = new GifBitmapEncoder();
                break;

            case ".bmp":
                encoder = new BmpBitmapEncoder();
                break;

            case ".png":
                encoder = new PngBitmapEncoder();
                break;

            case ".wdp":
                encoder = new WmpBitmapEncoder();
                break;

            case ".tif":
                encoder = new TiffBitmapEncoder();
                break;
            }
            try
            {
                encoder.Frames.Add(BitmapFrame.Create(bitmap));
                using (Stream s = File.Create(path))
                {
                    encoder.Save(s);
                }
            }
            catch
            {
                MessageBox.Show("File could not be saved. Please make sure the path you entered is correct", "Error");
            }
        }