简体   繁体   English

面板上的C#呼吸描记器...如何另存为BMP?

[英]C# spirograph on a panel… how to save as BMP?

I am using Graphics on a Panel to generate a spirograph, but when I try to save the drawing as a BMP (or whatever file extension), the file only takes the background of the panel, but the spirograph is not there! 我正在使用Panel上的Graphics生成呼吸描记器,但是当我尝试将图形另存为BMP(或任何文件扩展名)时,该文件仅使用面板的背景,但是呼吸描记器不存在! Anyone knows a way around? 有人知道解决方法吗?

        int rayonA = Convert.ToInt32(txtCercleFixe.Text);
        int rayonB = Convert.ToInt32(txtCercleNonFixe.Text);
        int distance = Convert.ToInt32(txtPenDistance.Text);
        int pointsParCourbe = Convert.ToInt32(txtPointsParCourbe.Text);
        int TypeCourbe = 0;


        Graphics dessin = pnlSpiro.CreateGraphics();

public void DessinHypotrochoid(ref Graphics dessin, PointF ptOrigin, int rayonA, int rayonB, int distance, int pointParCourbe, int PFC, int rouge, int vert, int bleu,bool random)
    {
        // Dim angleStep As Double = radiansPerCircle / PointsPerCurve
        double angleStep = radians / pointParCourbe;

        //' Compute number of revolutions.
        //Dim NumRevolutions As Integer = (bRadius / HighestCommonFactor(Math.Round(aRadius), Math.Round(bRadius)))
        int NumRevolution = rayonB / PFC;

        //' Total number of points to generate
        //Nombre de points totaux à générer
        //Dim NumPoints As Integer = PointsPerCurve * NumRevolutions
        int NumPoints = pointParCourbe * NumRevolution;

        //Dim oldPoint As New PointF( _
        // ptOrigin.X + aRadius - bRadius + distance, ptOrigin.Y)
        PointF oldPoint = new PointF((ptOrigin.X + rayonA - rayonB + distance), ptOrigin.Y);

        //Dim angle As Double = 0
        double angle = 0;
        //Dim aMinusb As Double = aRadius - bRadius
        double aMoinsB = rayonA - rayonB;
        //Dim aMinusbOverb As Double = aMinusb / bRadius
        double aDiviseB = aMoinsB / rayonB;
        //Dim pt As Integer
        //For pt = 0 To NumPoints - 1
        // On fait le dessin.

        for (int pt = 0; pt <= NumPoints; pt += 1)
        {
            angle += angleStep;
            PointF newPoint = new PointF((float)(ptOrigin.X + aMoinsB * Math.Cos(angle) + distance * Math.Cos(angle * aDiviseB)),(float)(ptOrigin.Y + aMoinsB * Math.Sin(angle) - distance * Math.Sin(angle * aDiviseB)));
            if (pt == 0)
            {
                oldPoint = newPoint;
            }
            if (random == false)
            {
                Pen Pinceau = new Pen(Color.FromArgb(rouge, vert, bleu), 1);
                dessin.DrawLine(Pinceau, oldPoint, newPoint);
            }
            else
            {
                Random r = new Random();
                Pen Pinceau = new Pen(Color.FromArgb(r.Next(0, 256), r.Next(0, 256), r.Next(0, 256)), 1);
                dessin.DrawLine(Pinceau, oldPoint, newPoint);
            }

            oldPoint = newPoint;
        }
        dessin.Flush();
        dessin.Dispose();
    }

You cannot save bitmaps directly from the Graphics object of the Panel. 您不能直接从面板的Graphics对象保存位图。

First, you have to create a Bitmap object and derive Graphics from it. 首先,您必须创建一个位图对象并从中获取图形。 Then, once the drawing is finished, you can reuse the bitmap as you wish : either save it to the disk or show it in a picturebox, or both ! 然后,一旦完成绘图,就可以根据需要重复使用位图:将其保存到磁盘上或在图片框中显示,或两者!

This article will show you detailed information about how to do this. 本文将向您显示有关如何执行此操作的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM