这是代码。 但它并没有将bmp4染成黄色。 也许我对测试变量和bmp4变量以及CreateNonIndexedImage方法做错了什么? 试图保存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一起使用时,这就是我得到的以及我现在想要得到的东西:
答案 0 :(得分:0)
在 bmp Bitmap :
上从黄色列表中绘制单个像素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);