How to color/highlight the clouds in yellow only when the FillPie(cone shape) is above clouds?

Daniel Lee 61 Reputation points
2022-12-02T17:37:35.103+00:00

The method FindPoints detect clouds in weather radar image. The FillPie graphics object is rotating 360 degrees nonstop.
i want to do that when the FillPie(cone) is above any clouds(cloudPoints) color in yellow this clouds that the FillPie(cone) is currently above on.

like a weather doppler radar effect.

screenshot of the image in pictureBox1 with the weather radar and the doppler fillpie(cone) rotating 360 degrees :

266731-rad1.jpg

this screenshot is showing what i mean by highlight/color in the yellow the clouds that the FillPie(cone) is above on : no much clouds in this image so on the right i resized it larger to see it :

266665-rad2.jpg

and this screenshot is just for testing to show how it looks like when detecting the clouds this is the file bmptest1.bmp i save in the FindPoints method.

266649-rad3.png

This is the full code it's all in form1 and in this version of my code i'm using GraphicsPath and then SetPixels and this is working to highlight/color the clouds when the FillPie(cone) is above,
The problem with that version is that the two loops and the SetPixels make it all very slow. the FillPie(cone) almost not rotating because it's so slow.

so i wonder how to convert the SetPixels part to using LockBits and make that part faster ?

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Drawing.Drawing2D;  
using System.Drawing.Imaging;  
using System.IO;  
using System.Linq;  
using System.Runtime.InteropServices.ComTypes;  
using System.Text;  
using System.Threading;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
  
namespace Doppler_Radar  
{  
    public partial class Form1 : Form  
    {  
        int myPiePercent = 15;  
        float distanceFromCenterPixels;  
        float distanceFromCenterKm = 200F;  
        Stream mymem;  
        List<Point> cloudPoints = new List<Point>();  
        GraphicsPath graphicsPath;  
        Bitmap bmptest;  
  
        public Form1()  
        {  
            InitializeComponent();  
  
            graphicsPath = new GraphicsPath();  
            pictureBox1.Image = Image.FromFile(@"D:\New folder (4)\Weather Radar\WithClouds.bmp");  
            timer1.Enabled = true;  
            distanceFromCenterPixels = (float)(183d * (double)distanceFromCenterKm / 200d);  
            mymem = ToStream(pictureBox1.Image, ImageFormat.Bmp);  
            FindPoints();  
        }  
  
        public static Stream ToStream(Image image, ImageFormat formaw)  
        {  
            var stream = new System.IO.MemoryStream();  
            image.Save(stream, formaw);  
            stream.Position = 0;  
            return stream;  
        }  
  
        private void pictureBox1_Paint(object sender, PaintEventArgs e)  
        {  
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;  
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;  
            e.Graphics.DrawRectangle(Pens.Green, 0, 0, pictureBox1.Width - 1, pictureBox1.Height - 1);  
  
            Pen p = new Pen(Color.Red);  
            e.Graphics.DrawLine(p, 256, 0, 256, 512);  
            e.Graphics.DrawLine(p, 0, 256, 512, 256);  
  
            FindPoints();  
            DrawPieOnPicturebox(e.Graphics);  
        }  
  
        public void DrawPieOnPicturebox(Graphics myPieGraphic)  
        {  
            Color myPieColors = Color.FromArgb(150, Color.LightGreen);  
            Size myPieSize = new Size((int)distanceFromCenterPixels, (int)distanceFromCenterPixels);  
            Point myPieLocation = new Point((pictureBox1.Width - myPieSize.Width) / 2, (pictureBox1.Height - myPieSize.Height) / 2);  
            DrawMyPie(myPiePercent, myPieColors, myPieGraphic, myPieLocation, myPieSize);  
        }  
  
        public void DrawMyPie(int myPiePerecent, Color myPieColor, Graphics myPieGraphic, Point  
      myPieLocation, Size myPieSize)  
        {  
            using (SolidBrush brush = new SolidBrush(myPieColor))  
            {  
                myPieGraphic.FillPie(brush, new Rectangle(myPieLocation, myPieSize), Convert.ToSingle(myPiePerecent * 360 / 100), Convert.ToSingle(15 * 360 / 100));  
            }  
  
            myPieGraphic.DrawImage(bmptest, Point.Empty);  
            graphicsPath.AddPie(new Rectangle(myPieLocation, myPieSize), Convert.ToSingle(myPiePerecent * 360 / 100), Convert.ToSingle(15 * 360 / 100));  
        }  
  
  
        private void timer1_Tick(object sender, EventArgs e)  
        {  
            myPiePercent++;  
            pictureBox1.Invalidate();  
        }  
  
        private void FindPoints()  
        {  
              
            GraphicsPath gp = new GraphicsPath();  
            int x, y, p, j, wdthHght;  
            int bytes;  
            byte[] rgbValuesWithClouds;  
            byte[] rgbValuesWithoutClouds;  
            IntPtr ptr;  
            Rectangle rect;  
            BitmapData bitmap_Data;  
  
            Bitmap bmpWithClouds; //No memory is allocated  
            Bitmap bmpWithoutClouds; //No memory is allocated  
  
            gp.AddEllipse(new RectangleF(73, 72, 367, 367));  
  
            //using the using statement, bmpWithClouds bitmap is automatically disposed at the end of statement. No memory leaks :)  
            using (bmpWithClouds = new Bitmap(mymem))  
            {  
                rect = new Rectangle(0, 0, bmpWithClouds.Width, bmpWithClouds.Height);  
                wdthHght = bmpWithClouds.Width;  
  
                //Lock bitmap to copy its color information fast  
                bitmap_Data = bmpWithClouds.LockBits(rect, ImageLockMode.ReadWrite, bmpWithClouds.PixelFormat);  
                ptr = bitmap_Data.Scan0;  
                bytes = bitmap_Data.Stride * bmpWithClouds.Height;  
  
                rgbValuesWithClouds = new byte[bytes];  
  
                //copy color information to rgbValues array  
                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValuesWithClouds, 0, bytes);  
                //we are done copying so unlock bitmap. We dont need it anymore  
                bmpWithClouds.UnlockBits(bitmap_Data);  
            }  
  
            //using the using statement, bmpWithClouds bitmap is automatically disposed at the end of statement. No memory leaks :)  
            using (bmpWithoutClouds = new Bitmap(@"F:\Drive Backup\Samsung Galaxy S9\Danny Backup\Recovered data 02-10 18_32_36\1  (D) NTFS\C-Sharp\Weather Radar\WithoutClouds.bmp")) //24 bit bitmap  
            {  
                rect = new Rectangle(0, 0, bmpWithoutClouds.Width, bmpWithoutClouds.Height);  
  
                //Lock bitmap to copy its color information fast  
                bitmap_Data = bmpWithoutClouds.LockBits(rect, ImageLockMode.ReadWrite, bmpWithoutClouds.PixelFormat);  
                ptr = bitmap_Data.Scan0;  
                bytes = bitmap_Data.Stride * bmpWithoutClouds.Height;  
  
                rgbValuesWithoutClouds = new byte[bytes];  
  
                //copy color information to rgbValues array  
                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValuesWithoutClouds, 0, bytes);  
                //we are done copying so unlock bitmap. We dont need it anymore  
                bmpWithoutClouds.UnlockBits(bitmap_Data);  
            }  
  
            // Each position in these arrays, rgbValuesWithoutClouds and rgbValuesWithClouds, corresponds a color. eg  
            // First pixel   Second pixel   Third pixel   Forth pixel .... // bitmaps  
            //    B G R          B G R         B G R         B G R    .... // rgbValues arrays  
            bmptest = new Bitmap(512, 512);  
            cloudPoints = new List<Point>();  
  
            for (y = 0; y < wdthHght; y++)  
            {  
                j = 0;  
                for (x = 0; x < wdthHght; x++)  
                {  
                    p = y * wdthHght * 3 + j;  
  
                    if (rgbValuesWithClouds[p] != rgbValuesWithoutClouds[p])  
                    {  
                        cloudPoints.Add(new Point(x, y));  
  
                        if (graphicsPath.IsVisible(x, y))  
                        {  
                            bmptest.SetPixel(x, y, Color.Yellow);  
                        }    
                    }  
  
                    j += 3;  
                }  
            }  
            bmptest.Save(@"d:\bmptest1.bmp");  
        }  
    }  
}  
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,838 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,292 questions
{count} votes