Add DrawText to ImageHelper

This commit is contained in:
2025-06-17 12:48:35 -04:00
parent 25f655302f
commit 9bab83d587

View File

@@ -131,9 +131,26 @@ namespace MarketData.CNNProcessing
this.pointMapping=pointMapping;
bitmap=new SKBitmap(width,height,SKColorType.Rgba8888, SKAlphaType.Premul);
Validate();
}
}
/// <summary>
/// Create a new 32 bit bitmap with 8 bits per channel which includes 8 bits for the alpha channel where 0 = fully transparent and 255 = fully opaque
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public void CreateImage(int width, int height)
{
DisposeAll();
this.pointMapping = new PointMapping(width, height, width, 0, height, 0, 0,0);
bitmap=new SKBitmap(width,height,SKColorType.Rgba8888, SKAlphaType.Premul);
Validate();
}
// This is the method that is currently being used in the CNNClient.Predict to send the image stream to Flask
/// <summary>
/// Save the bitmap as black and white jpeg and return the stream
/// This is the method that is currently being used in the CNNClient.Predict to send the image stream to Flask
/// </summary>
/// <returns></returns>
public Stream SaveBlackAndWhiteJPG()
{
Validate();
@@ -145,7 +162,11 @@ namespace MarketData.CNNProcessing
return memoryStream;
}
// Convert bitmap to stream for use in CNNClient
/// <summary>
/// Convert bitmap to stream for use in CNNClient
/// </summary>
/// <param name="pathFileName"></param>
/// <returns></returns>
public Stream ToStream()
{
Validate();
@@ -155,6 +176,11 @@ namespace MarketData.CNNProcessing
return memoryStream;
}
/// <summary>
/// Save the bitmap as gray scale jpeg to the specified file
/// </summary>
/// <param name="pathFileName"></param>
/// <returns></returns>
public bool SaveGrayScaleJPG(String pathFileName)
{
Validate();
@@ -164,6 +190,11 @@ namespace MarketData.CNNProcessing
return true;
}
/// <summary>
/// Save the bitmap as black and white jpeg to the specified file
/// </summary>
/// <param name="pathFileName"></param>
/// <returns></returns>
public bool SaveBlackAndWhiteJPG(String pathFileName)
{
Validate();
@@ -173,6 +204,10 @@ namespace MarketData.CNNProcessing
return true;
}
/// <summary>
/// Save the bitmap to the specified file
/// </summary>
/// <param name="pathFileName"></param>
public void Save(String pathFileName)
{
Save(pathFileName,null);
@@ -204,7 +239,11 @@ namespace MarketData.CNNProcessing
canvas.DrawBitmap(bitmap, new SKPoint(0,0), paint);
return dstBitmap;
}
/// <summary>
/// Rotates the bitmap right by 90 degrees
/// </summary>
/// <returns></returns>
public bool RotateRight()
{
Validate();
@@ -215,6 +254,10 @@ namespace MarketData.CNNProcessing
return true;
}
/// <summary>
/// Rotates the bitmap left by 90 degrees
/// </summary>
/// <returns></returns>
public bool RotateLeft()
{
Validate();
@@ -225,6 +268,12 @@ namespace MarketData.CNNProcessing
return true;
}
/// <summary>
/// Rotates the bitmap to the specified angle
/// </summary>
/// <param name="bitmap"></param>
/// <param name="angle"></param>
/// <returns></returns>
private SKBitmap Rotate(SKBitmap bitmap, double angle)
{
double radians = Math.PI * angle / 180;
@@ -256,7 +305,7 @@ namespace MarketData.CNNProcessing
bitmap=grayScaleBitmap;
}
// Convert to 8 bits per pixel black and white
// Convert to 8 bits per pixel black and white
private SKBitmap ToGrayScale(SKBitmap bitmap)
{
SKColorFilter grayScaleColorFilter = SKColorFilter.CreateColorMatrix(new float[]
@@ -275,6 +324,11 @@ namespace MarketData.CNNProcessing
return dstBitmap;
}
/// <summary>
/// Adds a blur effect to the bitmap
/// </summary>
/// <param name="sigmaX"></param>
/// <returns></returns>
public bool Blur(float sigmaX)
{
Validate();
@@ -283,7 +337,14 @@ namespace MarketData.CNNProcessing
bitmap = blurredBitmap;
return true;
}
/// <summary>
/// Applies a blur effect to the bitmap
/// </summary>
/// <param name="image"></param>
/// <param name="sigmaX"></param>
/// <param name="sigmaY"></param>
/// <returns></returns>
private SKBitmap Blur(SKBitmap image, float sigmaX=5.0f, float sigmaY=5.0f)
{
SKImageFilter imageFilter = SKImageFilter.CreateBlur(sigmaX, sigmaY);
@@ -405,19 +466,51 @@ namespace MarketData.CNNProcessing
}
}
public void DrawLine(SKColor color, float strokeWidth, SKPoint srcPoint,SKPoint dstPoint)
/// <summary>
/// Draw a line on the bitmap
/// </summary>
/// <param name="color"></param>
/// <param name="strokeWidth"></param>
/// <param name="srcPoint"></param>
/// <param name="dstPoint"></param>
public void DrawLine(SKColor color, float strokeWidth, SKPoint srcPoint, SKPoint dstPoint)
{
Validate();
SKPoint txSrcPoint=pointMapping.MapPoint(srcPoint);
SKPoint txDstPoint=pointMapping.MapPoint(dstPoint);
SKPoint txSrcPoint = pointMapping.MapPoint(srcPoint);
SKPoint txDstPoint = pointMapping.MapPoint(dstPoint);
using SKCanvas canvas = new SKCanvas(bitmap);
using SKPaint paint = new SKPaint();
paint.Style = SKPaintStyle.Stroke;
paint.StrokeWidth = strokeWidth; // Set the desired stroke width
paint.Color = SKColors.Black;
paint.Color = color;
canvas.DrawLine(txSrcPoint, txDstPoint, paint);
}
/// <summary>
/// Draw text on the bitmap
/// </summary>
/// <param name="color"></param>
/// <param name="strokeWidth"></param>
/// <param name="srcPoint"></param>
/// <param name="dstPoint"></param>
public void DrawText(String text, SKPoint srcPoint,SKColor color,SKTextAlign align,SKFont font, float strokeWidth=1)
{
Validate();
SKPoint txSrcPoint = pointMapping.MapPoint(srcPoint);
using SKCanvas canvas = new SKCanvas(bitmap);
using SKPaint paint = new SKPaint();
paint.Style = SKPaintStyle.Stroke;
paint.StrokeWidth = strokeWidth; // Set the desired stroke width
paint.Color = color;
canvas.DrawText(text,srcPoint, align, font, paint);
}
/// <summary>
/// Draws the path along the line segments
/// </summary>
/// <param name="color"></param>
/// <param name="strokeWidth"></param>
/// <param name="lineSegments"></param>
public void DrawPath(SKColor color, float strokeWidth, LineSegments lineSegments)
{
Validate();
@@ -434,11 +527,18 @@ namespace MarketData.CNNProcessing
canvas.DrawLine(txSrcPoint, txDstPoint, paint);
}
}
public bool DrawCircle(SKColor color, SKPoint center, float radius=1.00f)
/// <summary>
/// Draws a circle on the bitmap
/// </summary>
/// <param name="color"></param>
/// <param name="center"></param>
/// <param name="radius"></param>
/// <returns></returns>
public bool DrawCircle(SKColor color, SKPoint center, float radius = 1.00f)
{
Validate();
SKPoint txPointCenter=pointMapping.MapPoint(center);
SKPoint txPointCenter = pointMapping.MapPoint(center);
using SKPaint paint = new SKPaint();
paint.Color = color;
paint.Style = SKPaintStyle.Fill;