本文整理汇总了C#中CGBitmapContext.SetGrayFillColor方法的典型用法代码示例。如果您正苦于以下问题:C# CGBitmapContext.SetGrayFillColor方法的具体用法?C# CGBitmapContext.SetGrayFillColor怎么用?C# CGBitmapContext.SetGrayFillColor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CGBitmapContext
的用法示例。
在下文中一共展示了CGBitmapContext.SetGrayFillColor方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddImageReflection
public static UIImage AddImageReflection(UIImage image, float reflectionFraction)
{
int reflectionHeight = (int) (image.Size.Height * reflectionFraction);
// Create a 2 bit CGImage containing a gradient that will be used for masking the
// main view content to create the 'fade' of the reflection. The CGImageCreateWithMask
// function will stretch the bitmap image as required, so we can create a 1 pixel wide gradient
// gradient is always black and white and the mask must be in the gray colorspace
var colorSpace = CGColorSpace.CreateDeviceGray ();
// Creat the bitmap context
var gradientBitmapContext = new CGBitmapContext (IntPtr.Zero, 1, reflectionHeight, 8, 0, colorSpace, CGImageAlphaInfo.None);
// define the start and end grayscale values (with the alpha, even though
// our bitmap context doesn't support alpha the gradien requires it)
float [] colors = { 0, 1, 1, 1 };
// Create the CGGradient and then release the gray color space
var grayScaleGradient = new CGGradient (colorSpace, colors, null);
colorSpace.Dispose ();
// create the start and end points for the gradient vector (straight down)
var gradientStartPoint = new PointF (0, reflectionHeight);
var gradientEndPoint = PointF.Empty;
// draw the gradient into the gray bitmap context
gradientBitmapContext.DrawLinearGradient (grayScaleGradient, gradientStartPoint,
gradientEndPoint, CGGradientDrawingOptions.DrawsAfterEndLocation);
grayScaleGradient.Dispose ();
// Add a black fill with 50% opactiy
gradientBitmapContext.SetGrayFillColor (0, 0.5f);
gradientBitmapContext.FillRect (new RectangleF (0, 0, 1, reflectionHeight));
// conver the context into a CGImage and release the context
var gradientImageMask = gradientBitmapContext.ToImage ();
gradientBitmapContext.Dispose ();
// create an image by masking the bitmap of the mainView content with the gradient view
// then release the pre-masked content bitmap and the gradient bitmap
var reflectionImage = image.CGImage.WithMask (gradientImageMask);
gradientImageMask.Dispose ();
var size = new SizeF (image.Size.Width, image.Size.Height + reflectionHeight);
UIGraphics.BeginImageContext (size);
image.Draw (PointF.Empty);
var context = UIGraphics.GetCurrentContext ();
context.DrawImage (new RectangleF (0, image.Size.Height, image.Size.Width, reflectionHeight), reflectionImage);
var result = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
reflectionImage.Dispose ();
return result;
}
示例2: Texture2D
public Texture2D(string text, SizeF dim, UITextAlignment alignment, string fontName, float fontSize)
{
UIFont font = UIFont.FromName(fontName, fontSize);
int width = (int)dim.Width;
if (width != 1 && (width & (width - 1)) != 0) {
int i = 1;
while (i < width) {
i *= 2;
}
width = i;
}
int height = (int)dim.Height;
if (height != 1 && (height & (height - 1)) != 0) {
int i = 1;
while (i < height) {
i *= 2;
}
height = i;
}
CGColorSpace colorSpace = CGColorSpace.CreateDeviceGray();
byte[] data = new byte[width * height];
unsafe {
fixed (byte* dataPb = data) {
using (CGContext context = new CGBitmapContext((IntPtr)dataPb, width, height, 8, width, colorSpace, CGImageAlphaInfo.None)) {
context.SetGrayFillColor(1f, 1f);
context.TranslateCTM(0f, height);
context.ScaleCTM(1f, -1f);
UIGraphics.PushContext(context);
text.DrawInRect(new RectangleF(0, 0, dim.Width, dim.Height), font, UILineBreakMode.WordWrap, alignment);
UIGraphics.PopContext();
}
}
}
colorSpace.Dispose();
InitWithData(data, Texture2DPixelFormat.A8, width, height, dim);
}