2016-07-23 24 views
0

你好,我想獲得在Xamarin C#觸摸像素的顏色, 但我得到錯誤的值有時,我得到R = 255,G = 0 ,B = 0,A = 0 我的代碼有什麼問題?如何獲得正確的像素點在觸摸在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; 
    } 

回答

0

使用UIColor.FromRGBA而不是new UIColor(....)

這是我該怎麼辦:

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

一些奇怪的原因導致返回RGB值是0,如果你點擊圖像的特定區域外,反正謝謝你的答案:) – vin

相關問題