C# 如何更快地为绘制事件中的点着色?

C# 如何更快地为绘制事件中的点着色?,c#,.net,winforms,C#,.net,Winforms,这是代码。 但它并没有把bmp4染成黄色。 也许我对测试变量、bmp4变量和CreateNonIndexeImage方法做错了什么? 还试图保存bmp4,但它不起作用 在form1中,当操作结束时,我将按如下方式保存位图: CloudEnteringAlert.test.Save(@"c:\temp\yellowbmpcolor.jpg"); 但是应该是黄色的区域不是,它和原始图像一样 using System; using System.Collections.Generic; using

这是代码。 但它并没有把bmp4染成黄色。 也许我对测试变量、bmp4变量和CreateNonIndexeImage方法做错了什么? 还试图保存bmp4,但它不起作用

在form1中,当操作结束时,我将按如下方式保存位图:

CloudEnteringAlert.test.Save(@"c:\temp\yellowbmpcolor.jpg");
但是应该是黄色的区域不是,它和原始图像一样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.IO;
using DannyGeneral;
using System.Reflection;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;



namespace mws
{
    public static class CloudEnteringAlert
    {



        private static PointF point1;
        private static PointF point2;
        public static float redlinerectx1= 0;
        public static float redlinerecty1 = 0;
        public static float redlinerectx = 0;
        public static float redlinerecty = 0;
        private static Bitmap bm;
        private static Bitmap bmp2 = new Bitmap(@"C:\Temp\New folder (17)\radar001486.GIF");
        public static Bitmap test = new Bitmap(@"D:\MyWeatherStation-Images-And-Icons\radar090.PNG");
        public static Bitmap bmp4 = CreateNonIndexedImage(test);
        static BitmapData b1 = bmp4.LockBits(new System.Drawing.Rectangle(0, 0, bmp4.Width, bmp4.Height),
                             System.Drawing.Imaging.ImageLockMode.ReadWrite,
                             System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        static int stride = b1.Stride;
        static int k1, x1, y1;
        static float fx, fy;
        static System.IntPtr Scan0 = b1.Scan0;
        public static List<string> pointscoordinates = new List<string>();
        public static float radius = 2.0f;
        private static Label lbl1 = new Label();
        public static bool cloudsfound;
        // blinking colors: yellow, red, yellow, transparent, repeat...
        public static Brush[] cloudColors = new[] { Brushes.Yellow, Brushes.Transparent };
        // current color index
        public static int cloudColorIndex = 0;
        public static List<PointF> AddDistanceToPoints = new List<PointF>();
        public static List<PointF> MovingPoints = new List<PointF>();
        public static List<PointF> PointsFloat = new List<PointF>();
        public static List<Point> PointsInt;
        public static List<PointF> pointtocolor = new List<PointF>();
        public static List<PointF> extendedPoints = new List<PointF>();
        public static Bitmap newbitmap;
        private static List<PointF> clouds1;
        public static List<PointF> clouds;
        public static List<PointF> cloudsG = new List<PointF>();
        public static List<PointF> cloudsY;
        public static List<PointF> cloudsR;
        private static Bitmap bmp3;
        private static  int tolerancenumeric = 0;
        private static double[] treshhold_array = { 100, 50, 40, 30, 24, 18, 13, 9, 6, 4, 2, 1.2, 0.7, 0.2, 0.1 };
        static List<float> LoadPoints_X = new List<float>();
        static List<float> LoadPoints_Y = new List<float>();
        static List<float> points_X = new List<float>();
        static List<float> points_Y = new List<float>();
        static string path;
        static string file;
        static List<PointF> points;

        public static Bitmap CreateNonIndexedImage(Image src)
        {
            Bitmap newBmp = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            using (Graphics gfx = Graphics.FromImage(newBmp))
            {
                gfx.DrawImage(src, 0, 0);
            }

            return newBmp;
        }

        static CloudEnteringAlert()
        {
            points = new List<PointF>();
            cloudsfound = false;
        }

        public static void Paint(Graphics e, double currentFactor, float kilometers)
        {
            Pen myPen = new Pen(Brushes.Red, 2);
            float x, y ,width, height;
            float distance = kilometers / (float)1.09;//289617486; // One pixel distance is 1.09 kilometer.

            if (points == null)
            {
                return;
            }

             redlinerectx = MovingPoints[0].X;
             redlinerecty = MovingPoints[0].Y;

             if (clouds != null)
             {
                 y = PointsInt.Min(p => p.Y);
                 PointF pointsIntMin = PointsInt.First(p => p.Y == y); //point with minimum Y in PointsInt list
                 y = PointsInt.Max(p => p.Y);
                 PointF pointsIntMax = PointsInt.First(p => p.Y == y); //point with maximum Y in PointsInt list

                 y = MovingPoints.Min(p => p.Y);
                 PointF movingPointsMin = MovingPoints.First(p => p.Y == y); //point with minimum Y in MovingPoints list
                 y = MovingPoints.Max(p => p.Y);
                 PointF movingPointsMax = PointsInt.First(p => p.Y == y); //point with minimum Y in MovingPoints list

                 x = pointsIntMin.X * (float)currentFactor;
                 y = pointsIntMin.Y * (float)currentFactor;
                 width = movingPointsMin.X + distance - x;
                 height = (pointsIntMax.Y - pointsIntMin.Y) * (float)currentFactor;

                 if (clouds != null)
                 {
                     e.DrawRectangle(myPen, (int)x, (int)y, (int)width, (int)height);
                 }

                 myPen.Dispose();

                 try
                 {
                     unsafe
                     {
                         byte* p;

                         for (k1 = 0; k1 < pointtocolor.Count; k1++)
                         {
                             //set pointer to the beggining
                             p = (byte*)(void*)Scan0;
                             fx = pointtocolor[k1].X * (float)currentFactor;
                             fy = pointtocolor[k1].Y * (float)currentFactor;
                             //check if point is inside bmp
                             if ((int)fx >= bmp.Width || (int)fy >= bmp.Height)
                             {
                                 continue;
                             }
                             //Add offset where the point is. The formula: position = Y * stride + X * 4 
                             x = (int)(fy * (float)stride);
                             y = (int)(fx * 4F);

                             p += (x1 + y1);

                             //set yellow color
                             p[1] = p[2] = (byte)255;
                             p[0] = (byte)0;
                             p[3] = (byte)255;
                         }
                     }

                     bmp.UnlockBits(b1);

                 }
                 catch
                 {
                     string t = "err";
                 }
             }
        }
编辑

它工作正常,但颜色不是我需要的黄色: 这就是我得到的:

当我使用SetPixel时,这就是我得到的,也是我现在想要得到的:


在bmp位图上用黄色从列表中绘制单个像素:


既然半径为2f,为什么首先要选择圆角椭圆呢?正如TaW所说。例如,使用FillRectangle可能更合理?是的,SetPixel非常慢,而且一直如此。你可以用很多方法来解决这个问题,例如,在字节[]或uint[]中完成所有工作,然后在数据上创建位图。不,我不是这个意思。我会选择锁定位,简单地画一个2f大小的圆圈,最多可能是5个全彩和4个混合色,总共可能只有4个。。这样,所有发生的事情都在快速锁位域中。。但当然,我们也可以通过一个接一个地进行两个不相互交织的绘制操作来混合这两个域:首先画这些椭圆,然后转到单个像素的锁位。在这一行我得到错误:p+=x+y;错误34无法将类型“float”隐式转换为“byte*”,错误33运算符“+=”不能应用于类型“byte*”和“float”的操作数,在这一部分:4F我得到错误:错误29无效表达式项,错误32仅赋值、调用、递增、递减、等待,新的对象表达式可以用作语句。行上仍然存在两个错误:p+=x+y;不能为字节和浮点应用+=γηράσκωδ′αεπλλλάΔασκμε你能看看我问题中的代码吗。也许我做错了什么。我使用了不安全的方法,就像您在Paint method中使用的方法一样。我在form1 Paint event中调用了该Paint方法。但最后,当我将图像保存到硬盘时,我看不到黄色的特定区域。它看起来就像是原始图像。我没有收到任何错误,只是不是黄色的。我用代码更新了我的问题,是问题,不是整个代码,只是与黄色的ocloring相关的问题。
BitmapData b1 = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),  
                             System.Drawing.Imaging.ImageLockMode.ReadWrite,
                             System.Drawing.Imaging.PixelFormat.Format32bppArgb);

int stride = b1.Stride;
int k, x, y;
float fx, fy;
System.IntPtr Scan0 = b1.Scan0;

unsafe
{
    byte* p;

    for (k = 0; k < pointtocolor.Count; k++)
    {
        //set pointer to the beggining
        p = (byte*)(void*)Scan0;
        fx = pointtocolor[k].X * (float)currentFactor;
        fy = pointtocolor[k].Y * (float)currentFactor;
        //check if point is inside bmp
        if( (int)fx >= bmp.Width || (int)fy >= bmp.Height)
        {
            continue;
        }             
        //Add offset where the point is. The formula: position = Y * stride + X * 4 
        x = (int)fy * stride;
        y = (int)fx * 4;

        p += ( x + y );

        //set yellow color
        p[1] = p[2] = (byte)255;
        p[0] = (byte)0;
        p[3] = (byte)255;
    }
}

bmp.UnlockBits(b1);