Fix Headlines entry UTF8 issue.

Fix issue with ImageHelper GrayScale.
This commit is contained in:
2024-04-24 13:35:31 -04:00
parent 2c7f7c0b42
commit 3e0f783278
3 changed files with 51 additions and 50 deletions

View File

@@ -121,38 +121,6 @@ namespace MarketData.CNNProcessing
}
}
/// <summary>
/// Resize the image to the specified width and height.
/// </summary>
/// <param name="image">The image to resize.</param>
/// <param name="width">The width to resize to.</param>
/// <param name="height">The height to resize to.</param>
/// <returns>The resized image.</returns>
//public static Bitmap ResizeImage(Image image, int width, int height)
//{
// var destRect = new Rectangle(0, 0, width, height);
// var destImage = new Bitmap(width, height);
// destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
// using (var graphics = Graphics.FromImage(destImage))
// {
// graphics.CompositingMode = CompositingMode.SourceCopy;
// graphics.CompositingQuality = CompositingQuality.HighQuality;
// graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// graphics.SmoothingMode = SmoothingMode.HighQuality;
// graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
// using (var wrapMode = new ImageAttributes())
// {
// wrapMode.SetWrapMode(WrapMode.TileFlipXY);
// graphics.DrawImage(image, destRect, 0, 0, image.Width,image.Height, GraphicsUnit.Pixel, wrapMode);
// }
// }
// return destImage;
//}
public void CreateImage(int width, int height,PointMapping pointMapping)
{
this.width=width;
@@ -172,13 +140,16 @@ namespace MarketData.CNNProcessing
bwBitmap.Dispose();
return memoryStream;
}
public void SaveGrayScaleJPG(String pathFileName)
{
Validate();
Bitmap bwBitmap=ToBlackAndWhite(bitmap);
Bitmap bwBitmap=ToGrayScale(bitmap);
Save(pathFileName,bwBitmap);
bwBitmap.Dispose();
}
// This is the method that is currently being used in the CNNProcessor to generate images
public void SaveBlackAndWhiteJPG(String pathFileName)
{
Validate();
@@ -196,6 +167,7 @@ namespace MarketData.CNNProcessing
if(null==altBitmap)BitmapExtensions.SaveJPG100(bitmap,pathFileName);
else BitmapExtensions.SaveJPG100(altBitmap,pathFileName);
}
// Convert to 8 bits per pixel black and white
private Bitmap ToBlackAndWhite(Bitmap bmp)
{
@@ -217,26 +189,46 @@ namespace MarketData.CNNProcessing
result.UnlockBits(data);
return result;
}
// Convert to 8 bits per pixel gray scale
// Convert to 8 bits per pixel gray scale
// private Bitmap ToGrayScale(Bitmap bmp)
//{
// var result = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
// BitmapData data = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
// byte[] bytes = new byte[data.Height * data.Stride];
// Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
// for (int y = 0; y < bmp.Height; y++)
// {
// for (int x = 0; x < bmp.Width; x++)
// {
// Color color = bmp.GetPixel(x, y);
// byte rgb = (byte)((color.R + color.G + color.B) / 3);
// bytes[y * data.Stride + x] = rgb;
// }
// }
// Marshal.Copy(bytes, 0, data.Scan0, bytes.Length);
// result.UnlockBits(data);
// return result;
// }
private Bitmap ToGrayScale(Bitmap bmp)
{
var result = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
BitmapData data = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
byte[] bytes = new byte[data.Height * data.Stride];
Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
Color color = bmp.GetPixel(x, y);
byte rgb = (byte)((color.R + color.G + color.B) / 3);
bytes[y * data.Stride + x] = rgb;
}
{
Bitmap bitmap = new Bitmap(bmp.Width, bmp.Height);
for (int i = 0; i < bmp.Width; i++)
{
for (int x = 0; x < bmp.Height; x++)
{
Color oc = bmp.GetPixel(i, x);
int grayScale = (int)((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11));
Color nc = Color.FromArgb(oc.A, grayScale, grayScale, grayScale);
bitmap.SetPixel(i, x, nc);
}
Marshal.Copy(bytes, 0, data.Scan0, bytes.Length);
result.UnlockBits(data);
return result;
}
return bitmap;
}
public Color GetPixel(int x,int y)
{
return bitmap.GetPixel(x,y);

View File

@@ -1712,6 +1712,8 @@ namespace MarketData.Helper
headline.Entry=headline.Entry.Replace("\\"," ");
headline.Entry=headline.Entry.Trim();
headline.Entry=Uri.UnescapeDataString(headline.Entry);
headline.Entry=Encoding.UTF8.GetString(Encoding.Default.GetBytes(headline.Entry));
headline.Entry=Utility.RemoveHtml(headline.Entry);
headline.Source = "Seeking Alpha";
headlines.Add(headline);
}
@@ -1781,6 +1783,7 @@ namespace MarketData.Helper
else headline = new Headline(symbol, Utility.ParseDate(keyValue.Key.Substring(0, keyValue.Key.IndexOf('T'))), keyValue.Value);
headline.Entry=headline.Entry.Replace("\\"," ");
headline.Entry=headline.Entry.Trim();
headline.Entry=Encoding.UTF8.GetString(Encoding.Default.GetBytes(headline.Entry));
headline.Entry=Uri.UnescapeDataString(headline.Entry);
headline.Source = "Seeking Alpha";
headlines.Add(headline);

View File

@@ -115,6 +115,11 @@ namespace MarketData.Utils
{
return (new WindowsPrincipal(WindowsIdentity.GetCurrent())).IsInRole(WindowsBuiltInRole.Administrator);
}
public static String ToUTF8(String str)
{
if(null==str)return str;
return Encoding.UTF8.GetString(Encoding.Default.GetBytes(str));
}
public static String RemoveHtml(String strItem)
{
if(null==strItem)return null;
@@ -133,6 +138,7 @@ namespace MarketData.Utils
strItem=strItem.Replace("&#x201D;",@"""");
strItem=strItem.Replace("&#x201d;",@"""");
strItem=strItem.Replace("&#x2013;","-");
strItem=strItem.Replace("&#39;","'");
return strItem;
}
public static String RemoveDivs(String strItem)