本文整理汇总了C#中CGBitmapContext.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# CGBitmapContext.Dispose方法的具体用法?C# CGBitmapContext.Dispose怎么用?C# CGBitmapContext.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CGBitmapContext
的用法示例。
在下文中一共展示了CGBitmapContext.Dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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 ();
}
示例2: 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;
}
示例3: GLTexture
public GLTexture (string inFilename)
{
GL.Enable (EnableCap.Texture2D);
GL.Enable (EnableCap.Blend);
filename = inFilename;
GL.Hint (HintTarget.GenerateMipmapHint, HintMode.Nicest);
GL.GenTextures (1, out texture);
GL.BindTexture (TextureTarget.Texture2D, texture);
GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) All.Repeat);
GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) All.Repeat);
GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) All.Linear);
GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) All.Linear);
GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) All.Nearest);
GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) All.Nearest);
//TODO Remove the Substring method if you don't support iOS versions prior to iOS 6.
string extension = Path.GetExtension (filename).Substring(1);
string baseFilename = Path.GetFileNameWithoutExtension (filename);
string path = NSBundle.MainBundle.PathForResource (baseFilename, extension);
NSData texData = NSData.FromFile (path);
UIImage image = UIImage.LoadFromData (texData);
if (image == null)
return;
nint width = image.CGImage.Width;
nint 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 CGRect (0, 0, width, height));
context.DrawImage (new CGRect (0, 0, width, height), image.CGImage);
GL.TexImage2D (TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, (int)width, (int)height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, imageData);
context.Dispose ();
}
示例4: TexImage2D
public static void TexImage2D(Stream data, out int width, out int height)
{
data.Position = 0;
var nsData = NSData.FromStream(data);
var image = UIImage.LoadFromData(nsData);
if (image == null) throw new Exception ("could not load image data");
width = (int)image.CGImage.Width;
height = (int)image.CGImage.Height;
var colorSpace = CGColorSpace.CreateDeviceRGB();
var imageData = new byte[height * width * 4];
var context = new CGBitmapContext (imageData, width, height, 8, 4 * width, colorSpace, CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big);
colorSpace.Dispose();
context.ClearRect(new CGRect(0, 0, width, height));
context.DrawImage(new CGRect(0, 0, width, height), image.CGImage);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, imageData);
context.Dispose();
}
示例5: InitWithCGImage
private void InitWithCGImage(CGImage image, All filter)
{
int width,height,i;
CGContext context = null;
IntPtr data;
CGColorSpace colorSpace;
IntPtr tempData;
bool hasAlpha;
CGImageAlphaInfo info;
CGAffineTransform transform;
Size imageSize;
SurfaceFormat pixelFormat;
bool sizeToFit = false;
if(image == null)
{
throw new ArgumentException(" uimage is invalid! " );
}
info = image.AlphaInfo;
hasAlpha = ((info == CGImageAlphaInfo.PremultipliedLast) || (info == CGImageAlphaInfo.PremultipliedFirst) || (info == CGImageAlphaInfo.Last) || (info == CGImageAlphaInfo.First) ? true : false);
if (image.ColorSpace != null)
{
pixelFormat = SurfaceFormat.Color;
}
else
{
pixelFormat = SurfaceFormat.Alpha8;
}
imageSize = new Size(image.Width,image.Height);
transform = CGAffineTransform.MakeIdentity();
width = imageSize.Width;
if((width != 1) && ((width & (width - 1))!=0)) {
i = 1;
while((sizeToFit ? 2 * i : i) < width)
i *= 2;
width = i;
}
height = imageSize.Height;
if((height != 1) && ((height & (height - 1))!=0)) {
i = 1;
while((sizeToFit ? 2 * i : i) < height)
i *= 2;
height = i;
}
// TODO: kMaxTextureSize = 1024
while((width > 1024) || (height > 1024))
{
width /= 2;
height /= 2;
transform = CGAffineTransform.MakeScale(0.5f,0.5f);
imageSize.Width /= 2;
imageSize.Height /= 2;
}
switch(pixelFormat)
{
case SurfaceFormat.Color:
colorSpace = CGColorSpace.CreateDeviceRGB();
data = Marshal.AllocHGlobal(height * width * 4);
context = new CGBitmapContext(data, width, height, 8, 4 * width, colorSpace,CGImageAlphaInfo.PremultipliedLast);
colorSpace.Dispose();
break;
case SurfaceFormat.Alpha8:
data = Marshal.AllocHGlobal(height * width);
context = new CGBitmapContext(data, width, height, 8, width, null, CGImageAlphaInfo.Only);
break;
default:
throw new NotSupportedException("Invalid pixel format");
}
context.ClearRect(new RectangleF(0,0,width,height));
context.TranslateCTM(0, height - imageSize.Height);
if (!transform.IsIdentity)
{
context.ConcatCTM(transform);
}
context.DrawImage(new RectangleF(0, 0, image.Width, image.Height), image);
//Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB"
/*
if(pixelFormat == SurfaceFormat.Rgb32) {
tempData = Marshal.AllocHGlobal(height * width * 2);
int d32;
short d16;
int inPixel32Count=0,outPixel16Count=0;
for(i = 0; i < width * height; ++i, inPixel32Count+=sizeof(int))
{
d32 = Marshal.ReadInt32(data,inPixel32Count);
short R = (short)((((d32 >> 0) & 0xFF) >> 3) << 11);
short G = (short)((((d32 >> 8) & 0xFF) >> 2) << 5);
short B = (short)((((d32 >> 16) & 0xFF) >> 3) << 0);
d16 = (short) (R | G | B);
Marshal.WriteInt16(tempData,outPixel16Count,d16);
//.........这里部分代码省略.........
示例6: ESTexture2D
public ESTexture2D(UIImage uiImage, All filter)
{
CGImage image = uiImage.CGImage;
if(uiImage == null)
throw new ArgumentNullException("uiImage");
// TODO: could use this to implement lower-bandwidth textures
//bool hasAlpha = (image.AlphaInfo == CGImageAlphaInfo.First || image.AlphaInfo == CGImageAlphaInfo.Last
// || image.AlphaInfo == CGImageAlphaInfo.PremultipliedFirst || image.AlphaInfo == CGImageAlphaInfo.PremultipliedLast);
// Image dimentions:
logicalSize = new Point((int)uiImage.Size.Width, (int)uiImage.Size.Height);
pixelWidth = uiImage.CGImage.Width;
pixelHeight = uiImage.CGImage.Height;
// Round up the target texture width and height to powers of two:
potWidth = pixelWidth;
potHeight = pixelHeight;
if(( potWidth & ( potWidth-1)) != 0) { int w = 1; while(w < potWidth) { w *= 2; } potWidth = w; }
if((potHeight & (potHeight-1)) != 0) { int h = 1; while(h < potHeight) { h *= 2; } potHeight = h; }
// Scale down textures that are too large...
CGAffineTransform transform = CGAffineTransform.MakeIdentity();
while((potWidth > 1024) || (potHeight > 1024))
{
potWidth /= 2; // Note: no precision loss - it's a power of two
potHeight /= 2;
pixelWidth /= 2; // Note: precision loss - assume possibility of dropping a pixel at each step is ok
pixelHeight /= 2;
transform.Multiply(CGAffineTransform.MakeScale(0.5f, 0.5f));
}
RecalculateRatio();
lock(textureLoadBufferLockObject)
{
CreateTextureLoadBuffer();
unsafe
{
fixed(byte* data = textureLoadBuffer)
{
var colorSpace = CGColorSpace.CreateDeviceRGB();
var context = new CGBitmapContext(new IntPtr(data), potWidth, potHeight,
8, 4 * potWidth, colorSpace, CGImageAlphaInfo.PremultipliedLast);
context.ClearRect(new RectangleF(0, 0, potWidth, potHeight));
context.TranslateCTM(0, potHeight - pixelHeight); // TODO: this does not play nice with the precision-loss above (keeping half-pixel to the edge)
if(!transform.IsIdentity)
context.ConcatCTM(transform);
context.DrawImage(new RectangleF(0, 0, image.Width, image.Height), image);
SetupTexture(new IntPtr(data), filter);
context.Dispose();
colorSpace.Dispose();
}
}
}
}
示例7: applyMosaic
static CGImage applyMosaic(int tileSize, List<Color> colorPalette, UIImage resizedImg, Bitmap bitmap)
{
// - Parameters
int width = tileSize; // tile width
int height = tileSize; // tile height
int outWidth = (int)(resizedImg.Size.Width - (resizedImg.Size.Width % width)); // Round image size
int outHeight = (int)(resizedImg.Size.Height - (resizedImg.Size.Height % height));
// -- Initialize buffer
CGBitmapContext context = new CGBitmapContext (System.IntPtr.Zero, // data
(int)outWidth, // width
(int)outHeight, // height
8, // bitsPerComponent
outWidth * 4, // bytesPerRow based on pixel width
CGColorSpace.CreateDeviceRGB (), // colorSpace
CGImageAlphaInfo.NoneSkipFirst);
// bitmapInfo
for (int yb = 0; yb < outHeight / height; yb++) {
for (int xb = 0; xb < outWidth / width; xb++) {
// -- Do the average colors on the source image for the
// corresponding mosaic square
int r_avg = 0;
int g_avg = 0;
int b_avg = 0;
for (int y = yb * height; y < (yb * height) + height; y++) {
for (int x = xb * width; x < (xb * width) + width; x++) {
Color c = bitmap.GetPixel (x, y);
// Retrieve color values of the source image
r_avg += c.R;
g_avg += c.G;
b_avg += c.B;
}
}
// Make average of R,G and B on filter size
r_avg = r_avg / (width * height);
g_avg = g_avg / (width * height);
b_avg = b_avg / (width * height);
// Find the nearest color in the palette
Color mosaicColor = new Color ();
double minDistance = int.MaxValue;
foreach (Color c in colorPalette) {
double distance = Math.Abs (Math.Pow (r_avg - c.R, 2) + Math.Pow (g_avg - c.G, 2) + Math.Pow (b_avg - c.B, 2));
if (distance < minDistance) {
mosaicColor = c;
minDistance = distance;
}
}
// Apply mosaic
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
context.SetFillColor (new CGColor (mosaicColor.R / 255f, mosaicColor.G / 255f, mosaicColor.B / 255f));
context.FillRect (new RectangleF (xb * width, yb * height, width, height));
}
}
}
}
//-- image from buffer
CGImage flippedImage = context.ToImage ();
context.Dispose ();
return flippedImage;
}
示例8: Texture2D
public Texture2D(UIImage uiImage)
{
if (uiImage == null) {
throw new ArgumentNullException("uiImage");
}
CGImage image = uiImage.CGImage;
if (image == null) {
throw new InvalidOperationException("Attempted to create a Texture2D from UIImage, but resulting CGImage is null");
}
CGImageAlphaInfo info = image.AlphaInfo;
bool hasAlpha = info == CGImageAlphaInfo.PremultipliedLast || info == CGImageAlphaInfo.PremultipliedFirst || info == CGImageAlphaInfo.Last || info == CGImageAlphaInfo.First;
int bpp = image.BitsPerComponent;
Texture2DPixelFormat pixelFormat;
if (image.ColorSpace != null) {
if (hasAlpha || bpp >= 8) {
pixelFormat = Texture2DPixelFormat.Default;
} else {
pixelFormat = Texture2DPixelFormat.RGB565;
}
} else {
pixelFormat = Texture2DPixelFormat.A8;
}
int width = image.Width;
if (width != 1 && (width & (width - 1)) != 0) {
int i = 1;
while (i < width) {
i *= 2;
}
width = i;
}
int height = image.Height;
if (height != 1 && (height & (height - 1)) != 0) {
int i = 1;
while (i < height) {
i *= 2;
}
height = i;
}
if (width > MaxTextureSize || height > MaxTextureSize) {
throw new InvalidOperationException("Image is too large. Width or height larger than MaxTextureSize");
}
CGColorSpace colorSpace = null;
CGContext context;
byte[] data;
unsafe {
// all formats require w*h*4, except A8 requires just w*h
int dataSize = width * height * 4;
if (pixelFormat == Texture2DPixelFormat.A8) {
dataSize = width * height;
}
data = new byte[dataSize];
fixed (byte* dp = data) {
switch (pixelFormat) {
case Texture2DPixelFormat.RGBA8888:
case Texture2DPixelFormat.RGBA4444:
case Texture2DPixelFormat.RGB5A1:
colorSpace = CGColorSpace.CreateDeviceRGB();
context = new CGBitmapContext((IntPtr)dp, (int)width, (int)height, 8, 4 * (int)width, colorSpace, CGImageAlphaInfo.PremultipliedLast);
break;
case Texture2DPixelFormat.RGB565:
colorSpace = CGColorSpace.CreateDeviceRGB();
context = new CGBitmapContext((IntPtr)dp, (int)width, (int)height, 8, 4 * (int)width, colorSpace, CGImageAlphaInfo.NoneSkipLast);
break;
case Texture2DPixelFormat.A8:
context = new CGBitmapContext((IntPtr)dp, (int)width, (int)height, 8, (int)width, null, CGImageAlphaInfo.Only);
break;
default:
throw new InvalidEnumArgumentException("pixelFormat", (int)pixelFormat, typeof(Texture2DPixelFormat));
}
if (colorSpace != null) {
colorSpace.Dispose();
}
context.ClearRect(new RectangleF(0, 0, width, height));
context.TranslateCTM(0, height - image.Height);
// why is this here? make an identity transform, then immediately not use it? Need to look into this
CGAffineTransform transform = CGAffineTransform.MakeIdentity();
if (!transform.IsIdentity) {
context.ConcatCTM(transform);
}
context.DrawImage(new RectangleF(0, 0, image.Width, image.Height), image);
}
}
//.........这里部分代码省略.........
示例9: InitWithCGImage
private void InitWithCGImage(CGImage image)
{
int width, height;
CGBitmapContext bitmap = null;
bool hasAlpha;
CGImageAlphaInfo alphaInfo;
CGColorSpace colorSpace;
int bitsPerComponent, bytesPerRow;
CGBitmapFlags bitmapInfo;
bool premultiplied = false;
int bitsPerPixel = 0;
if (image == null) {
throw new ArgumentException (" image is invalid! " );
}
alphaInfo = image.AlphaInfo;
hasAlpha = ((alphaInfo == CGImageAlphaInfo.PremultipliedLast) || (alphaInfo == CGImageAlphaInfo.PremultipliedFirst) || (alphaInfo == CGImageAlphaInfo.Last) || (alphaInfo == CGImageAlphaInfo.First) ? true : false);
imageSize.Width = (int)image.Width;
imageSize.Height = (int)image.Height;
width = (int)image.Width;
height = (int)image.Height;
// Not sure yet if we need to keep the original image information
// before we change it internally. TODO look at what windows does
// and follow that.
bitmapInfo = image.BitmapInfo;
bitsPerComponent = (int)image.BitsPerComponent;
bitsPerPixel = (int)image.BitsPerPixel;
bytesPerRow = width * bitsPerPixel/bitsPerComponent;
int size = bytesPerRow * height;
colorSpace = image.ColorSpace;
// Right now internally we represent the images all the same
// I left the call here just in case we find that this is not
// possible. Read the comments for non alpha images.
if(colorSpace != null) {
if( hasAlpha ) {
premultiplied = true;
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
bitmapInfo = CGBitmapFlags.PremultipliedLast;
}
else
{
// even for images without alpha we will internally
// represent them as RGB with alpha. There were problems
// if we do not do it this way and creating a bitmap context.
// The images were not drawing correctly and tearing. Also
// creating a Graphics to draw on was a nightmare. This
// should probably be looked into or maybe it is ok and we
// can continue representing internally with this representation
premultiplied = true;
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
bitmapInfo = CGBitmapFlags.NoneSkipLast;
}
} else {
premultiplied = true;
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
bitmapInfo = CGBitmapFlags.NoneSkipLast;
}
bytesPerRow = width * bitsPerPixel/bitsPerComponent;
size = bytesPerRow * height;
bitmapBlock = Marshal.AllocHGlobal (size);
bitmap = new CGBitmapContext (bitmapBlock,
width, height,
bitsPerComponent,
bytesPerRow,
colorSpace,
bitmapInfo);
bitmap.ClearRect (new CGRect (0,0,width,height));
// We need to flip the Y axis to go from right handed to lefted handed coordinate system
var transform = new CGAffineTransform(1, 0, 0, -1, 0, image.Height);
bitmap.ConcatCTM(transform);
bitmap.DrawImage (new CGRect (0, 0, image.Width, image.Height), image);
var provider = new CGDataProvider (bitmapBlock, size, true);
NativeCGImage = new CGImage (width, height, bitsPerComponent,
bitsPerPixel, bytesPerRow,
colorSpace,
bitmapInfo,
provider, null, true, image.RenderingIntent);
colorSpace.Dispose();
bitmap.Dispose();
}
示例10: MakeRoundCornerImage
public static UIImage MakeRoundCornerImage(UIImage img, int cornerWidth, int cornerHeight)
{
UIImage newImage = null;
if (null != img)
{
var w = img.Size.Width;
var h = img.Size.Height;
CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
CGContext context = new CGBitmapContext(null, (int)w, (int)h, 8, (int)(4 * w), colorSpace, CGImageAlphaInfo.PremultipliedFirst);
context.BeginPath();
var rect = new RectangleF(0, 0, img.Size.Width, img.Size.Height);
AddRoundedRectToPath (context, rect, cornerWidth, cornerHeight);
context.ClosePath();
context.Clip();
var cgImage = img.CGImage;
context.DrawImage (new RectangleF(0, 0, w, h), cgImage);
cgImage.Dispose();
CGImage imageMasked = ((CGBitmapContext)context).ToImage();
context.Dispose();
colorSpace.Dispose();
newImage = new UIImage(imageMasked);
imageMasked.Dispose();
}
return newImage;
}
示例11: SetOriginalImage
public void SetOriginalImage(UIImage originalImage, CGRect cropFrame)
{
LoadIndicator.StartAnimating();
InvokeOnMainThread(() =>
{
CGImage imageRef = originalImage.CGImage;
UIImageOrientation imageOrientation = originalImage.Orientation;
if (imageRef == null)
return;
var bytesPerRow = 0;
var width = imageRef.Width;
var height = imageRef.Height;
var bitsPerComponent = imageRef.BitsPerComponent;
CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
CGBitmapFlags bitmapInfo = imageRef.BitmapInfo;
switch (imageOrientation)
{
case UIImageOrientation.RightMirrored:
case UIImageOrientation.LeftMirrored:
case UIImageOrientation.Right:
case UIImageOrientation.Left:
width = imageRef.Height;
height = imageRef.Width;
break;
default:
break;
}
CGSize imageSize = new CGSize(width, height);
CGBitmapContext context = new CGBitmapContext(null,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpace,
bitmapInfo);
colorSpace.Dispose();
if (context == null)
{
imageRef.Dispose();
return;
}
switch (imageOrientation)
{
case UIImageOrientation.RightMirrored:
case UIImageOrientation.Right:
context.TranslateCTM(imageSize.Width / 2, imageSize.Height / 2);
context.RotateCTM(-((nfloat)Math.PI / 2));
context.TranslateCTM(-imageSize.Height / 2, -imageSize.Width / 2);
break;
case UIImageOrientation.LeftMirrored:
case UIImageOrientation.Left:
context.TranslateCTM(imageSize.Width / 2, imageSize.Height / 2);
context.RotateCTM((nfloat)(Math.PI / 2));
context.TranslateCTM(-imageSize.Height / 2, -imageSize.Width / 2);
break;
case UIImageOrientation.Down:
case UIImageOrientation.DownMirrored:
context.TranslateCTM(imageSize.Width / 2, imageSize.Height / 2);
context.RotateCTM((nfloat)Math.PI);
context.TranslateCTM(-imageSize.Width / 2, -imageSize.Height / 2);
break;
default:
break;
}
context.InterpolationQuality = CGInterpolationQuality.High;
context.SetBlendMode(CGBlendMode.Copy);
context.DrawImage(new CGRect(0, 0, imageRef.Width, imageRef.Height), imageRef);
CGImage contextImage = context.ToImage();
context.Dispose();
if (contextImage != null)
{
_originalImage = UIImage.FromImage(contextImage, originalImage.CurrentScale, UIImageOrientation.Up);
contextImage.Dispose();
}
imageRef.Dispose();
BeginInvokeOnMainThread(() =>
{
CGSize convertedImageSize = new CGSize(_originalImage.Size.Width / _cropSizeRatio,
_originalImage.Size.Height / _cropSizeRatio);
ImageView.Alpha = 0;
ImageView.Image = _originalImage;
CGSize sampleImageSize = new CGSize(Math.Max(convertedImageSize.Width, ScrollView.Frame.Size.Width),
Math.Max(convertedImageSize.Height, ScrollView.Frame.Size.Height));
//.........这里部分代码省略.........
示例12: ProcessedImage
public UIImage ProcessedImage()
{
nfloat scale = UIScreen.MainScreen.Scale;
CGImage imageRef = _originalImage.CGImage;
if (imageRef == null)
return null;
var bytesPerRow = 0;
var bitsPerComponent = imageRef.BitsPerComponent;
CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
var bitmapInfo = imageRef.BitmapInfo;
CGBitmapContext context = new CGBitmapContext(null,
(nint)(_targetSize.Width * scale),
(nint)(_targetSize.Height * scale),
bitsPerComponent,
bytesPerRow,
colorSpace,
bitmapInfo);
colorSpace.Dispose();
if (context == null)
{
imageRef.Dispose();
return null;
}
CGRect targetFrame = LocalCropFrame();
context.InterpolationQuality = CGInterpolationQuality.High;
context.SetBlendMode(CGBlendMode.Copy);
context.DrawImage(targetFrame, imageRef);
var contextImage = context.ToImage();
UIImage finalImage = null;
context.Dispose();
if (contextImage != null)
{
finalImage = UIImage.FromImage(contextImage, scale, UIImageOrientation.Up);
contextImage.Dispose();
}
imageRef.Dispose();
return finalImage;
}
示例13: GetCharData
public Size GetCharData(char c,out UInt32[] cdata)
{
NSString str =new NSString(c.ToString());
var size=str.StringSize(font);
UIGraphics.BeginImageContextWithOptions(size,false,1.0f);
UIGraphics.GetCurrentContext().SetFillColor(1.0f,1.0f,1.0f,1.0f);
str.DrawString(new System.Drawing.PointF(0,0),font);
UIImage img =UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
//
int width=(int)size.Width;
int height=(int)size.Height;
cdata=new UInt32[width*height];
var gdata=new byte[width*height*4];
var colorSpace = CGColorSpace.CreateDeviceRGB();
var gbmp = new CGBitmapContext(gdata, width, height,
8, width * 4, colorSpace, CGBitmapFlags.PremultipliedLast);
//gbmp.ClearRect(new RectangleF(0,0,width,height));
gbmp.DrawImage(new System.Drawing.RectangleF(0,0,width,height),img.CGImage);
gbmp.Dispose();
colorSpace.Dispose();
unsafe
{
fixed(byte* srcb = gdata)
fixed (UInt32* dest=cdata)
{
UInt32* src=(UInt32*)srcb;
for(int y=0;y<height;y++)
{
for(int x=0;x<width;x++)
{
dest[y*width+x]=src[y*width+x];
}
}
}
}
return new Size(width,height);
}
示例14: Scale
public static CGImage Scale (CGImage image, Rectangle drawRect, Size size)
{
var bytesPerRow = (size.Width * 4);
var totalBytes = (bytesPerRow * size.Height);
IntPtr bitmapData = IntPtr.Zero;
CGBitmapContext context = null;
CGImage outImage;
try {
bitmapData = Marshal.AllocHGlobal (totalBytes);
if (bitmapData == IntPtr.Zero) {
return null;
}
using (var colorSpace = CGColorSpace.CreateDeviceRGB ()) {
context = new CGBitmapContext (
bitmapData, size.Width, size.Height, 8, bytesPerRow,
colorSpace, CGImageAlphaInfo.NoneSkipFirst
);
}
if (context == null)
return null;
context.DrawImage (drawRect, image);
outImage = context.ToImage ();
} catch {
return null;
} finally {
if (context != null)
context.Dispose ();
Marshal.FreeHGlobal (bitmapData);
}
return outImage;
}
示例15: Finalize
public bool Finalize (IMTLDevice device)
{
if (MetalTexture != null)
return true;
UIImage image = UIImage.FromFile (path);
if (image == null)
return false;
using (CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB ()) {
if (colorSpace == null)
return false;
Width = image.CGImage.Width;
Height = image.CGImage.Height;
nuint width = (nuint)Width;
nuint height = (nuint)Height;
nuint rowBytes = width * 4;
var context = new CGBitmapContext (IntPtr.Zero,
(int)width,
(int)height,
8,
(int)rowBytes,
colorSpace,
CGImageAlphaInfo.PremultipliedLast);
if (context == null)
return false;
var bounds = new CGRect (0f, 0f, width, height);
context.ClearRect (bounds);
// Vertical Reflect
if (flip) {
context.TranslateCTM (width, height);
context.ScaleCTM (-1f, -1f);
}
context.DrawImage (bounds, image.CGImage);
MTLTextureDescriptor texDesc = MTLTextureDescriptor.CreateTexture2DDescriptor (MTLPixelFormat.RGBA8Unorm, width, height, false);
if (texDesc == null)
return false;
MetalTexture = device.CreateTexture (texDesc);
if (MetalTexture == null) {
context.Dispose ();
return false;
}
IntPtr pixels = context.Data;
if (pixels != IntPtr.Zero) {
var region = new MTLRegion ();
region.Origin.X = 0;
region.Origin.Y = 0;
region.Size.Width = (nint)width;
region.Size.Height = (nint)height;
MetalTexture.ReplaceRegion (region, 0, pixels, rowBytes);
}
context.Dispose ();
}
return true;
}