简体   繁体   English

如何在iOS Xamarin C#中获得正确的触摸像素值

[英]How to get correct value of pixel on touch in ios xamarin c#

Hello i am trying to get the color of pixel on touch in Xamarin C#, but i get wrong values at times,i get R=255,G=0,B=0,A=0 What's wrong in my code? 您好,我试图在Xamarin C#中获得触摸时像素的颜色,但是有时我得到的值是错误的,我得到R = 255,G = 0,B = 0,A = 0我的代码有什么问题?

 private UIColor ColorOfPoint(CGPoint point)
    {
        byte[] pixel = {0,0,0,0};

        using(CGColorSpace oColorSpace = CGColorSpace.CreateDeviceRGB())
        using(CGBitmapContext oContext = new CGBitmapContext(pixel,
                                                     1, 1, 8, 4, oColorSpace, CGBitmapFlags.PremultipliedLast & CGBitmapFlags.AlphaInfoMask))
        {
            oContext.TranslateCTM(-point.X,-point.Y);
            img.Layer.RenderInContext(oContext);
            oContext.Dispose();
            oColorSpace.Dispose();
        }

        UIColor color = new UIColor(pixel[0]/255,pixel[1]/255,pixel[2]/255,pixel[3]/255);
        return color;
    }

Use UIColor.FromRGBA instead of new UIColor(....) 使用UIColor.FromRGBA代替new UIColor(....)

This is how I do: 这是我的方法:

byte[] alphaPixel = { 0, 0, 0, 0 };
protected UIColor GetColorAtTouchPoint(CGPoint point)
{
    var colorSpace = CGColorSpace.CreateDeviceRGB();
    var bitmapContext = new CGBitmapContext(alphaPixel, 1, 1, 8, 4, colorSpace, CGBitmapFlags.PremultipliedLast);
    bitmapContext.TranslateCTM(-point.X, -point.Y);
    view.Layer.RenderInContext(bitmapContext);
    return UIColor.FromRGBA(alphaPixel[0], alphaPixel[1], alphaPixel[2], alphaPixel[3]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM