C#でPNGの側面(左右上下)のパディング(クリアスペース)をプログラムで測定することは可能ですか? ピクセルにクリアまたは空でないものが含まれているかどうかを確認するために、側面から始めてピクセルごとに画像を解析できますか? 色ではなくピクセルが空であることをどのように判断しますか?
私の PNG は UIImageView にロードされますが、PNG または UIImage/UIImageView のいずれかを処理できます。これまでに機能するもの。
ここにPNGがあります
これが私がプログラムで測定したいものです。
-------------- ここに投稿されたソリューション ----------------
UIImage Image = UIImage.FromFile("image.png");
IntPtr bitmapData = RequestImagePixelData(Image);
PointF point = new PointF(100,100);
//Check for out of bounds
if(point.Y < 0 || point.X < 0 || point.Y > Image.Size.Height || point.X > Image.Size.Width)
{
Console.WriteLine("out of bounds!");
}
else
{
Console.WriteLine("in bounds!");
var startByte = (int) ((point.Y * Image.Size.Width + point.X) * 4);
byte alpha = GetByte(startByte, bitmapData);
Console.WriteLine("Alpha value of an image of size {0} at point {1}, {2} is {3}", Image.Size, point.X, point.Y, alpha);
}
protected IntPtr RequestImagePixelData(UIImage InImage)
{
CGImage image = InImage.CGImage;
int width = image.Width;
int height = image.Height;
CGColorSpace colorSpace = image.ColorSpace;
int bytesPerRow = image.BytesPerRow;
int bitsPerComponent = image.BitsPerComponent;
CGImageAlphaInfo alphaInfo = image.AlphaInfo;
IntPtr rawData;
CGBitmapContext context = new CGBitmapContext(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, alphaInfo);
context.SetBlendMode(CGBlendMode.Copy);
context.DrawImage(new RectangleF(0, 0, width, height), image);
return context.Data;
}
//Note: Unsafe code. Make sure to allow unsafe code in your
unsafe byte GetByte(int offset, IntPtr buffer)
{
byte* bufferAsBytes = (byte*) buffer;
return bufferAsBytes[offset];
}
明らかに、各ピクセルを解析し、クリア ピクセルが停止する場所を決定するロジックを作成する必要があります。そのロジックは非常に単純なので、あえて投稿しません。側面から始めて、ゼロではないアルファ値が見つかるまで進みます。
助けてくれてありがとう!