I am trying to draw text and get the (x,y)s of the text.
I will set the background to (R=0,B=0,G=0) and set the text Color to (R=255,G=255,B=255).
and I want to know which pixel is (R=255,G=255,B=255).
I tried several methods.
<code>UIGraphics.BeginImageContext(new CGSize(309, 60));
using (var gra = UIGraphics.GetCurrentContext())
{
gra.SetLineWidth(1);
var font = UIFont.SystemFontOfSize(13);
UIGraphics.PushContext(gra);
gra.SaveState();
var attributedString = new NSAttributedString("这是一个文本", new CTStringAttributes
{
Font = new CTFont(font.Name, 13),
ForegroundColor = UIColor.Red.CGColor,
StrokeColor = UIColor.FromRGB(255, 255, 255).CGColor
});
attributedString.DrawString(new CGRect(0, 0, 309, 60));
gra.RestoreState();
UIGraphics.PopContext();
var imgData= gra.AsBitmapContext().Data;
}
</code>
<code>UIGraphics.BeginImageContext(new CGSize(309, 60));
using (var gra = UIGraphics.GetCurrentContext())
{
gra.SetLineWidth(1);
var font = UIFont.SystemFontOfSize(13);
UIGraphics.PushContext(gra);
gra.SaveState();
var attributedString = new NSAttributedString("这是一个文本", new CTStringAttributes
{
Font = new CTFont(font.Name, 13),
ForegroundColor = UIColor.Red.CGColor,
StrokeColor = UIColor.FromRGB(255, 255, 255).CGColor
});
attributedString.DrawString(new CGRect(0, 0, 309, 60));
gra.RestoreState();
UIGraphics.PopContext();
var imgData= gra.AsBitmapContext().Data;
}
</code>
UIGraphics.BeginImageContext(new CGSize(309, 60));
using (var gra = UIGraphics.GetCurrentContext())
{
gra.SetLineWidth(1);
var font = UIFont.SystemFontOfSize(13);
UIGraphics.PushContext(gra);
gra.SaveState();
var attributedString = new NSAttributedString("这是一个文本", new CTStringAttributes
{
Font = new CTFont(font.Name, 13),
ForegroundColor = UIColor.Red.CGColor,
StrokeColor = UIColor.FromRGB(255, 255, 255).CGColor
});
attributedString.DrawString(new CGRect(0, 0, 309, 60));
gra.RestoreState();
UIGraphics.PopContext();
var imgData= gra.AsBitmapContext().Data;
}
but all the imgData’s pixel is #000000
there is no (R=255,G=255,B=255)pixel.
if I add gra.SetStrokeColor(UIColor.FromRGB(255,255,255).CGColor);
the all pixels will become (R=255,G=255,B=255).
I have tried to use glyph to draw the text but I got the same result.
how to get the pixels?
I will use this code :
<code>width = (int)gra.AsBitmapContext().Width;
height = (int)gra.AsBitmapContext().Height;
bytesPerPixel = (int)gra.AsBitmapContext().BytesPerRow / width;
bytesPerRow = (int)gra.AsBitmapContext().BytesPerRow;
int i = (bytesPerRow * y) + (x * bytesPerPixel);
var red = Marshal.ReadByte(raw) / 255.0f;
var green = Marshal.ReadByte(raw + 1) / 255.0f;
var blue = Marshal.ReadByte(raw + 2) / 255.0f;
</code>
<code>width = (int)gra.AsBitmapContext().Width;
height = (int)gra.AsBitmapContext().Height;
bytesPerPixel = (int)gra.AsBitmapContext().BytesPerRow / width;
bytesPerRow = (int)gra.AsBitmapContext().BytesPerRow;
int i = (bytesPerRow * y) + (x * bytesPerPixel);
var red = Marshal.ReadByte(raw) / 255.0f;
var green = Marshal.ReadByte(raw + 1) / 255.0f;
var blue = Marshal.ReadByte(raw + 2) / 255.0f;
</code>
width = (int)gra.AsBitmapContext().Width;
height = (int)gra.AsBitmapContext().Height;
bytesPerPixel = (int)gra.AsBitmapContext().BytesPerRow / width;
bytesPerRow = (int)gra.AsBitmapContext().BytesPerRow;
int i = (bytesPerRow * y) + (x * bytesPerPixel);
var red = Marshal.ReadByte(raw) / 255.0f;
var green = Marshal.ReadByte(raw + 1) / 255.0f;
var blue = Marshal.ReadByte(raw + 2) / 255.0f;