本文整理汇总了C#中CGBitmapContext.ToImage方法的典型用法代码示例。如果您正苦于以下问题:C# CGBitmapContext.ToImage方法的具体用法?C# CGBitmapContext.ToImage怎么用?C# CGBitmapContext.ToImage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CGBitmapContext
的用法示例。
在下文中一共展示了CGBitmapContext.ToImage方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ResizeImageIOS
public byte[] ResizeImageIOS(byte[] imageData, float size)
{
UIImage originalImage = ImageFromByteArray(imageData);
System.Diagnostics.Debug.Write("originalImage.Size.Height"+ originalImage.Size.Height + ", " + originalImage.Size.Width);
UIImageOrientation orientation = originalImage.Orientation;
float width = size;
float height = ((float)originalImage.Size.Height / (float)originalImage.Size.Width) * size;
System.Diagnostics.Debug.Write("new size" + width + ", " + height);
//create a 24bit RGB image
using (CGBitmapContext context = new CGBitmapContext(IntPtr.Zero,
(int)width, (int)height, 8,
(int)(4 * width), CGColorSpace.CreateDeviceRGB(),
CGImageAlphaInfo.PremultipliedFirst))
{
RectangleF imageRect = new RectangleF(0, 0, width, height);
// draw the image
context.DrawImage(imageRect, originalImage.CGImage);
UIKit.UIImage resizedImage = UIKit.UIImage.FromImage(context.ToImage(), 0, orientation);
// save the image as a jpeg
return resizedImage.AsJPEG().ToArray();
}
}
示例2: GetImageFromSampleBuffer
/// <summary>
/// Gets a single image frame from sample buffer.
/// </summary>
/// <returns>The image from sample buffer.</returns>
/// <param name="sampleBuffer">Sample buffer.</param>
private UIImage GetImageFromSampleBuffer(CMSampleBuffer sampleBuffer) {
// Get a pixel buffer from the sample buffer
using (var pixelBuffer = sampleBuffer.GetImageBuffer () as CVPixelBuffer) {
// Lock the base address
pixelBuffer.Lock ((CVPixelBufferLock)0);
// Prepare to decode buffer
var flags = CGBitmapFlags.PremultipliedFirst | CGBitmapFlags.ByteOrder32Little;
// Decode buffer - Create a new colorspace
using (var cs = CGColorSpace.CreateDeviceRGB ()) {
// Create new context from buffer
using (var context = new CGBitmapContext (pixelBuffer.BaseAddress,
pixelBuffer.Width,
pixelBuffer.Height,
8,
pixelBuffer.BytesPerRow,
cs,
(CGImageAlphaInfo)flags)) {
// Get the image from the context
using (var cgImage = context.ToImage ()) {
// Unlock and return image
pixelBuffer.Unlock ((CVPixelBufferLock)0);
return UIImage.FromImage (cgImage);
}
}
}
}
}
示例3: DrawScreen
protected void DrawScreen ()
{
// create our offscreen bitmap context
// size
CGSize bitmapSize = new CGSize (imageView.Frame.Size);
using (CGBitmapContext context = new CGBitmapContext (IntPtr.Zero, (int)bitmapSize.Width, (int)bitmapSize.Height, 8, (int)(4 * bitmapSize.Width), CGColorSpace.CreateDeviceRGB (), CGImageAlphaInfo.PremultipliedFirst)) {
// save the state of the context while we change the CTM
context.SaveState ();
// draw our circle
context.SetFillColor (1, 0, 0, 1);
context.TranslateCTM (currentLocation.X, currentLocation.Y);
context.RotateCTM (currentRotation);
context.ScaleCTM (currentScale, currentScale);
context.FillRect (new CGRect (-10, -10, 20, 20));
// restore our transformations
context.RestoreState ();
// draw our coordinates for reference
DrawCoordinateSpace (context);
// output the drawing to the view
imageView.Image = UIImage.FromImage (context.ToImage ());
}
}
示例4: ViewDidLoad
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// set the background color of the view to white
View.BackgroundColor = UIColor.White;
// instantiate a new image view that takes up the whole screen and add it to
// the view hierarchy
RectangleF imageViewFrame = new RectangleF (0, -NavigationController.NavigationBar.Frame.Height, View.Frame.Width, View.Frame.Height);
imageView = new UIImageView (imageViewFrame);
View.AddSubview (imageView);
// create our offscreen bitmap context
// size
SizeF bitmapSize = new SizeF (View.Frame.Size);
using (CGBitmapContext context = new CGBitmapContext (IntPtr.Zero
, (int)bitmapSize.Width, (int)bitmapSize.Height, 8
, (int)(4 * bitmapSize.Width), CGColorSpace.CreateDeviceRGB ()
, CGImageAlphaInfo.PremultipliedFirst)) {
// declare vars
UIImage apressImage = UIImage.FromFile ("Images/Icons/512_icon.png");
PointF imageOrigin = new PointF ((imageView.Frame.Width / 2) - (apressImage.CGImage.Width / 2), (imageView.Frame.Height / 2) - (apressImage.CGImage.Height / 2));
RectangleF imageRect = new RectangleF (imageOrigin.X, imageOrigin.Y, apressImage.CGImage.Width, apressImage.CGImage.Height);
// draw the image
context.DrawImage (imageRect, apressImage.CGImage);
// output the drawing to the view
imageView.Image = UIImage.FromImage (context.ToImage ());
}
}
示例5: CreateImage
public IImage CreateImage(Color[] colors, int width, double scale = 1.0)
{
var pixelWidth = width;
var pixelHeight = colors.Length / width;
var bitmapInfo = CGImageAlphaInfo.PremultipliedFirst;
var bitsPerComp = 8;
var bytesPerRow = width * 4;// ((4 * pixelWidth + 3)/4) * 4;
var colorSpace = CGColorSpace.CreateDeviceRGB ();
var bitmap = new CGBitmapContext (IntPtr.Zero, pixelWidth, pixelHeight, bitsPerComp, bytesPerRow, colorSpace, bitmapInfo);
var data = bitmap.Data;
unsafe {
fixed (Color *c = colors) {
for (var y = 0; y < pixelHeight; y++) {
var s = (byte*)c + 4*pixelWidth*y;
var d = (byte*)data + bytesPerRow*y;
for (var x = 0; x < pixelWidth; x++) {
var b = *s++;
var g = *s++;
var r = *s++;
var a = *s++;
*d++ = a;
*d++ = (byte)((r * a) >> 8);
*d++ = (byte)((g * a) >> 8);
*d++ = (byte)((b * a) >> 8);
}
}
}
}
var image = bitmap.ToImage ();
return new CGImageImage (image, scale);
}
示例6: ViewDidLoad
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// set the background color of the view to white
View.BackgroundColor = UIColor.White;
// instantiate a new image view that takes up the whole screen and add it to
// the view hierarchy
RectangleF imageViewFrame = new RectangleF (0, -NavigationController.NavigationBar.Frame.Height, View.Frame.Width, View.Frame.Height);
imageView = new UIImageView (imageViewFrame);
View.AddSubview (imageView);
// create our offscreen bitmap context
// size
SizeF bitmapSize = new SizeF (imageView.Frame.Size);
using (CGBitmapContext context = new CGBitmapContext (IntPtr.Zero,
(int)bitmapSize.Width, (int)bitmapSize.Height, 8,
(int)(4 * bitmapSize.Width), CGColorSpace.CreateDeviceRGB (),
CGImageAlphaInfo.PremultipliedFirst)) {
// draw our coordinates for reference
DrawCoordinateSpace (context);
// draw our flag
DrawFlag (context);
// add a label
DrawCenteredTextAtPoint (context, 384, 700, "Stars and Stripes", 60);
// output the drawing to the view
imageView.Image = UIImage.FromImage (context.ToImage ());
}
}
示例7: Draw
public override void Draw(CGRect rect)
{
base.Draw (rect);
var screenScale = UIScreen.MainScreen.Scale;
var width = (int)(Bounds.Width * screenScale);
var height = (int)(Bounds.Height * screenScale);
IntPtr buff = System.Runtime.InteropServices.Marshal.AllocCoTaskMem (width * height * 4);
try {
using (var surface = SKSurface.Create (width, height, SKColorType.N_32, SKAlphaType.Premul, buff, width * 4)) {
var skcanvas = surface.Canvas;
skcanvas.Scale ((float)screenScale, (float)screenScale);
using (new SKAutoCanvasRestore (skcanvas, true)) {
skiaView.SendDraw (skcanvas);
}
}
using (var colorSpace = CGColorSpace.CreateDeviceRGB ())
using (var bContext = new CGBitmapContext (buff, width, height, 8, width * 4, colorSpace, (CGImageAlphaInfo)bitmapInfo))
using (var image = bContext.ToImage ())
using (var context = UIGraphics.GetCurrentContext ()) {
// flip the image for CGContext.DrawImage
context.TranslateCTM (0, Frame.Height);
context.ScaleCTM (1, -1);
context.DrawImage (Bounds, image);
}
} finally {
if (buff != IntPtr.Zero)
System.Runtime.InteropServices.Marshal.FreeCoTaskMem (buff);
}
}
示例8: GrayscaleImage
/// <summary>
/// Creates grayscaled image from existing image.
/// </summary>
/// <param name="oldImage">Image to convert.</param>
/// <returns>Returns grayscaled image.</returns>
public static UIImage GrayscaleImage( UIImage oldImage )
{
var imageRect = new RectangleF(PointF.Empty, (SizeF) oldImage.Size);
CGImage grayImage;
// Create gray image.
using (CGColorSpace colorSpace = CGColorSpace.CreateDeviceGray()) {
using (var context = new CGBitmapContext(IntPtr.Zero, (int) imageRect.Width, (int) imageRect.Height, 8, 0, colorSpace, CGImageAlphaInfo.None)) {
context.DrawImage(imageRect, oldImage.CGImage);
grayImage = context.ToImage();
}
}
// Create mask for transparent areas.
using (var context = new CGBitmapContext(IntPtr.Zero, (int) imageRect.Width, (int) imageRect.Height, 8, 0, CGColorSpace.Null, CGBitmapFlags.Only)) {
context.DrawImage(imageRect, oldImage.CGImage);
CGImage alphaMask = context.ToImage();
var newImage = new UIImage(grayImage.WithMask(alphaMask));
grayImage.Dispose();
alphaMask.Dispose();
return newImage;
}
}
示例9: ResizeImage
public byte[] ResizeImage(byte[] imageData, float width, float height)
{
UIImage originalImage = ImageFromByteArray (imageData);
float oldWidth = (float)originalImage.Size.Width;
float oldHeight = (float)originalImage.Size.Height;
float scaleFactor = 0f;
if (oldWidth > oldHeight) {
scaleFactor = width / oldWidth;
} else {
scaleFactor = height / oldHeight;
}
float newHeight = oldHeight * scaleFactor;
float newWidth = oldWidth * scaleFactor;
//create a 24bit RGB image
using (CGBitmapContext context = new CGBitmapContext (null,
(int)newWidth, (int)newHeight, 8,
0, CGColorSpace.CreateDeviceRGB (), CGImageAlphaInfo.PremultipliedFirst)) {
RectangleF imageRect = new RectangleF (0, 0, newWidth, newHeight);
// draw the image
context.DrawImage (imageRect, originalImage.CGImage);
UIKit.UIImage resizedImage = UIKit.UIImage.FromImage (context.ToImage ());
// save the image as a jpeg
return resizedImage.AsJPEG ().ToArray ();
}
}
示例10: 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;
}
示例11: ConvertToGrayScale
UIImage ConvertToGrayScale(UIImage image)
{
RectangleF imageRect = new RectangleF (0, 0, (float)image.Size.Width, (float)image.Size.Height);
using (var colorSpace = CGColorSpace.CreateDeviceGray ())
using (var context = new CGBitmapContext (IntPtr.Zero, (int) imageRect.Width, (int) imageRect.Height, 8, 0, colorSpace, CGImageAlphaInfo.None)) {
context.DrawImage (imageRect, image.CGImage);
using (var imageRef = context.ToImage ())
return new UIImage (imageRef);
}
}
示例12: ConvertToGrayScale
public static UIImage ConvertToGrayScale (UIImage image)
{
var imageRect = new CGRect (CGPoint.Empty, image.Size);
using (var colorSpace = CGColorSpace.CreateDeviceGray ())
using (var context = new CGBitmapContext (IntPtr.Zero, (int) imageRect.Width, (int) imageRect.Height, 8, 0, colorSpace, CGImageAlphaInfo.None)) {
context.DrawImage (imageRect, image.CGImage);
using (var imageRef = context.ToImage ())
return new UIImage (imageRef);
}
}
示例13: MakeEmpty
static UIImage MakeEmpty ()
{
using (var cs = CGColorSpace.CreateDeviceRGB ()){
using (var bit = new CGBitmapContext (IntPtr.Zero, dimx, dimy, 8, 0, cs, CGImageAlphaInfo.PremultipliedFirst)){
bit.SetStrokeColor (1, 0, 0, 0.5f);
bit.FillRect (new RectangleF (0, 0, dimx, dimy));
return UIImage.FromImage (bit.ToImage ());
}
}
}
示例14: RgbaByteMatToCGImage
private static CGImage RgbaByteMatToCGImage(Mat bgraByte)
{
using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
using (CGBitmapContext context = new CGBitmapContext(
bgraByte.DataPointer,
bgraByte.Width, bgraByte.Height,
8,
bgraByte.Width*4,
cspace,
CGImageAlphaInfo.PremultipliedLast))
return context.ToImage();
}
示例15: ToColorSpace
public static UIImage ToColorSpace(UIImage source, CGColorSpace colorSpace)
{
CGRect bounds = new CGRect(0, 0, source.Size.Width, source.Size.Height);
using (var context = new CGBitmapContext(IntPtr.Zero, (int)bounds.Width, (int)bounds.Height, 8, 0, colorSpace, CGImageAlphaInfo.None))
{
context.DrawImage(bounds, source.CGImage);
using (var imageRef = context.ToImage())
{
return new UIImage(imageRef);
}
}
}