2016-07-23 19 views
0

Hallo, ich versuche, die Farbe des Pixels bei Berührung in Xamarin C#, zu bekommen, aber ich bekomme manchmal falsche Werte, ich bekomme R = 255, G = 0 , B = 0, A = 0 Was ist falsch in meinem Code?Wie bekomme ich den richtigen Wert von Pixel bei Berührung in Ios Xamarin C#

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; 
    } 

Antwort

0

Verwenden UIColor.FromRGBA statt new UIColor(....)

Dies ist, wie ich tun:

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]); 
} 
+0

unerfindlichen Gründen verursacht RGB-Werte 0 sein zurückgegeben werden, wenn Sie außerhalb eines bestimmten Bereichs des Bildes tippen, Anyways danke für die Antwort :) – vin