本文整理汇总了C#中CGBitmapContext类的典型用法代码示例。如果您正苦于以下问题:C# CGBitmapContext类的具体用法?C# CGBitmapContext怎么用?C# CGBitmapContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CGBitmapContext类属于命名空间,在下文中一共展示了CGBitmapContext类的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: Bitmap
public Bitmap(NSImage sourceImage)
{
Width = (int)sourceImage.CGImage.Width;
Height = (int)sourceImage.CGImage.Height;
_colors = new Color[Width * Height];
var rawData = new byte[Width * Height * 4];
var bytesPerPixel = 4;
var bytesPerRow = bytesPerPixel * Width;
var bitsPerComponent = 8;
using (var colorSpace = CGColorSpace.CreateDeviceRGB())
{
using (var context = new CGBitmapContext(rawData, Width, Height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapFlags.ByteOrder32Big | CGBitmapFlags.PremultipliedLast))
{
context.DrawImage(new CGRect(0, 0, Width, Height), sourceImage.CGImage);
for (int y = 0; y < Height; y++)
{
for (int x = 0; x < Width; x++)
{
var i = bytesPerRow * y + bytesPerPixel * x;
byte red = rawData[i + 0];
byte green = rawData[i + 1];
byte blue = rawData[i + 2];
byte alpha = rawData[i + 3];
SetPixel(x, y, Color.FromArgb(alpha, red, green, blue));
}
}
}
}
}
示例3: init
protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback)
{
try
{
using (var imageData = NSData.FromStream(stream))
using (var image = UIImage.LoadFromData(imageData))
{
int width = (int)image.Size.Width;
int height = (int)image.Size.Height;
Mipmaps = new Mipmap[1];
Size = new Size2(width, height);
var data = new byte[width * height * 4];
using (CGContext imageContext = new CGBitmapContext(data, width, height, 8, width*4, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedLast))
{
imageContext.DrawImage(new RectangleF(0, 0, width, height), image.CGImage);
Mipmaps[0] = new Mipmap(data, width, height, 1, 4);
if (flip) Mipmaps[0].FlipVertical();
}
}
}
catch (Exception e)
{
FailedToLoad = true;
Loader.AddLoadableException(e);
if (loadedCallback != null) loadedCallback(this, false);
return;
}
Loaded = true;
if (loadedCallback != null) loadedCallback(this, true);
}
示例4: iOSSurfaceSource
/// <summary>
/// Initializes a new instance of the <see cref="iOSSurfaceSource"/> class.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> that contains the surface data.</param>
public iOSSurfaceSource(Stream stream)
{
Contract.Require(stream, nameof(stream));
using (var data = NSData.FromStream(stream))
{
using (var img = UIImage.LoadFromData(data))
{
this.width = (Int32)img.Size.Width;
this.height = (Int32)img.Size.Height;
this.stride = (Int32)img.CGImage.BytesPerRow;
this.bmpData = Marshal.AllocHGlobal(stride * height);
using (var colorSpace = CGColorSpace.CreateDeviceRGB())
{
using (var bmp = new CGBitmapContext(bmpData, width, height, 8, stride, colorSpace, CGImageAlphaInfo.PremultipliedLast))
{
bmp.ClearRect(new CGRect(0, 0, width, height));
bmp.DrawImage(new CGRect(0, 0, width, height), img.CGImage);
}
}
}
}
ReversePremultiplication();
}
示例5: 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 ());
}
}
示例6: GetImagaDataFromPath
void GetImagaDataFromPath (string path)
{
NSImage src;
CGImage image;
CGContext context = null;
src = new NSImage (path);
var rect = CGRect.Empty;
image = src.AsCGImage (ref rect, null, null);
width =(int)image.Width;
height = (int) image.Height;
data = new byte[width * height * 4];
CGImageAlphaInfo ai = CGImageAlphaInfo.PremultipliedLast;
context = new CGBitmapContext (data, width, height, 8, 4 * width, image.ColorSpace, ai);
// Core Graphics referential is upside-down compared to OpenGL referential
// Flip the Core Graphics context here
// An alternative is to use flipped OpenGL texture coordinates when drawing textures
context.TranslateCTM (0, height);
context.ScaleCTM (1, -1);
// Set the blend mode to copy before drawing since the previous contents of memory aren't used.
// This avoids unnecessary blending.
context.SetBlendMode (CGBlendMode.Copy);
context.DrawImage (new CGRect (0, 0, width, height), image);
}
示例7: 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 ();
}
}
示例8: LoadBitmapData
void LoadBitmapData (int texId)
{
NSData texData = NSData.FromFile (NSBundle.MainBundle.PathForResource ("texture1", "png"));
UIImage image = UIImage.LoadFromData (texData);
if (image == null)
return;
int width = image.CGImage.Width;
int height = image.CGImage.Height;
CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB ();
byte[] imageData = new byte[height * width * 4];
CGContext context = new CGBitmapContext (imageData, width, height, 8, 4 * width, colorSpace,
CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big);
context.TranslateCTM (0, height);
context.ScaleCTM (1, -1);
colorSpace.Dispose ();
context.ClearRect (new RectangleF (0, 0, width, height));
context.DrawImage (new RectangleF (0, 0, width, height), image.CGImage);
GL.TexImage2D (TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, imageData);
context.Dispose ();
}
示例9: 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);
}
示例10: 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 ());
}
}
示例11: 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;
}
}
示例12: 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);
}
}
}
}
}
示例13: DrawPathAsBackground
// Draws our animation path on the background image, just to show it
protected void DrawPathAsBackground ()
{
// create our offscreen bitmap context
var bitmapSize = new SizeF (View.Frame.Size);
using (var context = new CGBitmapContext (
IntPtr.Zero,
(int)bitmapSize.Width, (int)bitmapSize.Height, 8,
(int)(4 * bitmapSize.Width), CGColorSpace.CreateDeviceRGB (),
CGImageAlphaInfo.PremultipliedFirst)) {
// convert to View space
var affineTransform = CGAffineTransform.MakeIdentity ();
// invert the y axis
affineTransform.Scale (1f, -1f);
// move the y axis up
affineTransform.Translate (0, View.Frame.Height);
context.ConcatCTM (affineTransform);
// actually draw the path
context.AddPath (animationPath);
context.SetStrokeColor (UIColor.LightGray.CGColor);
context.SetLineWidth (3f);
context.StrokePath ();
// set what we've drawn as the backgound image
backgroundImage.Image = UIImage.FromImage (context.ToImage());
}
}
示例14: CalculateLuminance
private void CalculateLuminance(UIImage d)
{
var imageRef = d.CGImage;
var width = imageRef.Width;
var height = imageRef.Height;
var colorSpace = CGColorSpace.CreateDeviceRGB();
var rawData = Marshal.AllocHGlobal(height * width * 4);
try
{
var flags = CGBitmapFlags.PremultipliedFirst | CGBitmapFlags.ByteOrder32Little;
var context = new CGBitmapContext(rawData, width, height, 8, 4 * width,
colorSpace, (CGImageAlphaInfo)flags);
context.DrawImage(new RectangleF(0.0f, 0.0f, (float)width, (float)height), imageRef);
var pixelData = new byte[height * width * 4];
Marshal.Copy(rawData, pixelData, 0, pixelData.Length);
CalculateLuminance(pixelData, BitmapFormat.BGRA32);
}
finally
{
Marshal.FreeHGlobal(rawData);
}
}
示例15: GetPixelColor
private UIColor GetPixelColor(CGPoint point, UIImage image)
{
var rawData = new byte[4];
var handle = GCHandle.Alloc(rawData);
UIColor resultColor = null;
try
{
using (var colorSpace = CGColorSpace.CreateDeviceRGB())
{
using (var context = new CGBitmapContext(rawData, 1, 1, 8, 4, colorSpace, CGImageAlphaInfo.PremultipliedLast))
{
context.DrawImage(new CGRect(-point.X, point.Y - image.Size.Height, image.Size.Width, image.Size.Height), image.CGImage);
float red = (rawData[0]) / 255.0f;
float green = (rawData[1]) / 255.0f;
float blue = (rawData[2]) / 255.0f;
float alpha = (rawData[3]) / 255.0f;
resultColor = UIColor.FromRGBA(red, green, blue, alpha);
}
}
}
finally
{
handle.Free();
}
return resultColor;
}