public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) { Bitmap finalBitmap; if (bitmap.Width != radius || bitmap.Height!= radius) finalBitmap = Bitmap.CreateScaledBitmap(bitmap, radius, radius, false); else finalBitmap = bitmap; Bitmap output = Bitmap.CreateBitmap(finalBitmap.Width, finalBitmap.Height, Bitmap.Config.Argb8888); Canvas canvas = new Canvas(output); Paint paint = new Paint(); Rect rect = new Rect(0, 0, finalBitmap.Width, finalBitmap.Height); paint.AntiAlias=true; paint.FilterBitmap=true; paint.Dither=true; canvas.DrawARGB(0, 0, 0, 0); paint.Color=Color.ParseColor("#BAB399"); canvas.DrawCircle(finalBitmap.Width / 2 + 0.7f, finalBitmap.Height / 2 + 0.7f, finalBitmap.Width / 2 + 0.1f, paint); paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcIn)); canvas.DrawBitmap(finalBitmap, rect, rect, paint); return output; }
// If you would like to create a circle of the image set pixels to half the width of the image. internal static Bitmap GetRoundedCornerBitmap(Bitmap bitmap, int pixels) { Bitmap output = null; try { output = Bitmap.CreateBitmap(bitmap.Width, bitmap.Height, Bitmap.Config.Argb8888); Canvas canvas = new Canvas(output); Color color = new Color(66, 66, 66); Paint paint = new Paint(); Rect rect = new Rect(0, 0, bitmap.Width, bitmap.Height); RectF rectF = new RectF(rect); float roundPx = pixels; paint.AntiAlias = true; canvas.DrawARGB(0, 0, 0, 0); paint.Color = color; canvas.DrawRoundRect(rectF, roundPx, roundPx, paint); paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcIn)); canvas.DrawBitmap(bitmap, rect, rect, paint); } catch (System.Exception err) { System.Console.WriteLine ("GetRoundedCornerBitmap Error - " + err.Message); } return output; }
protected override void OnDraw(Canvas canvas) { Android.Graphics.Drawable.Drawable drawable = GetDrawable(); if (drawable is BitmapDrawable) { RectF rectF = new RectF(drawable.GetBounds()); int restoreCount = canvas.SaveLayer(rectF, null, Canvas.AllSaveFlag); GetImageMatrix().MapRect(rectF); Paint paint = ((BitmapDrawable)drawable).GetPaint(); paint.SetAntiAlias(true); paint.SetColor(unchecked((int)(0xff000000))); canvas.DrawARGB(0, 0, 0, 0); canvas.DrawRoundRect(rectF, Radius, Radius, paint); Xfermode restoreMode = paint.GetXfermode(); paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcIn)); base.OnDraw(canvas); // Restore paint and canvas paint.SetXfermode(restoreMode); canvas.RestoreToCount(restoreCount); } else { base.OnDraw(canvas); } }
static Bitmap GetCroppedBitmap (Bitmap bmp, int radius) { Bitmap sbmp; if (bmp.Width != radius || bmp.Height != radius) sbmp = Bitmap.CreateScaledBitmap (bmp, radius, radius, false); else sbmp = bmp; var output = Bitmap.CreateBitmap (sbmp.Width, sbmp.Height, Bitmap.Config.Argb8888); var canvas = new Canvas (output); var paint = new Paint (); var rect = new Rect (0, 0, sbmp.Width, sbmp.Height); paint.AntiAlias = true; paint.FilterBitmap = true; paint.Dither = true; canvas.DrawARGB (0, 0, 0, 0); paint.Color = Color.ParseColor ("#BAB399"); canvas.DrawCircle (sbmp.Width / 2 + 0.7f, sbmp.Height / 2 + 0.7f, sbmp.Width / 2 + 0.1f, paint); paint.SetXfermode (new PorterDuffXfermode (PorterDuff.Mode.SrcIn)); canvas.DrawBitmap (sbmp, rect, rect, paint); return output; }
public override void Draw(Android.Graphics.Canvas canvas) { if (_reinitializeImages) { InitializeImages(); _reinitializeImages = false; } UpdateSizes(); // Clear the canvas canvas.DrawARGB(255, 255, 255, 255); if (_centerBitmap != null && _centerBitmap.Bitmap != null) { var dest = CalculateCentrationRect(_centerBitmap.Bitmap); canvas.DrawBitmap(_centerBitmap.Bitmap, dest.Left + _swipeCurrectXOffset, dest.Top, null); } else if (_centerBitmap != null) { DrawLoadingText(canvas, 0); } if (_leftBitmap != null && _leftBitmap.Bitmap != null) { var dest = CalculateCentrationRect(_leftBitmap.Bitmap); canvas.DrawBitmap(_leftBitmap.Bitmap, dest.Left + _swipeCurrectXOffset - this.Width, dest.Top, null); } else if (_leftBitmap != null) { DrawLoadingText(canvas, -Width); } if (_rightBitmap != null && _rightBitmap.Bitmap != null) { var dest = CalculateCentrationRect(_rightBitmap.Bitmap); canvas.DrawBitmap(_rightBitmap.Bitmap, dest.Left + _swipeCurrectXOffset + this.Width, dest.Top, null); } else if (_rightBitmap != null) { DrawLoadingText(canvas, Width); } }
public static Bitmap GetRoundedCornerBitmap(Bitmap bitmap) { Bitmap output = Bitmap.CreateBitmap(bitmap.Width, bitmap.Height,Bitmap.Config.Argb8888); Canvas canvas = new Canvas(output); //int color = 0xff424242; Paint paint = new Paint(); Rect rect = new Rect(0, 0, bitmap.Width, bitmap.Height); RectF rectF = new RectF(rect); float roundPx = 12.0f; paint.AntiAlias=true; canvas.DrawARGB(0, 0, 0, 0); paint.Color = Color.Red; //color;//Color.Transparent; canvas.DrawRoundRect(rectF, roundPx, roundPx, paint); paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcIn)); canvas.DrawBitmap(bitmap, rect, rect, paint); return output; }
public void InitWithBitmap(Bitmap imageSource, ALL11 filter) { //TODO: Android.Opengl.GLUtils.GetInternalFormat() _format = SurfaceFormat.Color; if (imageSource.HasAlpha) _format = SurfaceFormat.Color; if (GraphicsDevice.OpenGLESVersion == OpenTK.Graphics.GLContextVersion.Gles2_0) { _width = imageSource.Width; _height = imageSource.Height; } else { // scale up bitmap to be power of 2 dimensions but dont exceed 1024x1024. // Note: may not have to do this with OpenGL 2+ _width = (int)Math.Pow(2, Math.Min(10, Math.Ceiling(Math.Log10(imageSource.Width) / Math.Log10(2)))); _height = (int)Math.Pow(2, Math.Min(10, Math.Ceiling(Math.Log10(imageSource.Height) / Math.Log10(2)))); } _size.Width = _width; _size.Height = _height; using (Bitmap imagePadded = Bitmap.CreateBitmap(_width, _height, Bitmap.Config.Argb8888)) { Canvas can = new Canvas(imagePadded); can.DrawARGB(0, 0, 0, 0); can.DrawBitmap(imageSource, 0, 0, null); if (GraphicsDevice.OpenGLESVersion == OpenTK.Graphics.GLContextVersion.Gles2_0) { GL11.GenTextures(1, ref _name); GL11.BindTexture(ALL11.Texture2D, _name); GL11.TexParameter(ALL11.Texture2D, ALL11.TextureMinFilter, (int)filter); GL11.TexParameter(ALL11.Texture2D, ALL11.TextureMagFilter, (int)filter); Android.Opengl.GLUtils.TexImage2D((int)ALL11.Texture2D, 0, imagePadded, 0); // free bitmap imageSource.Recycle(); // error checking int errAndroidGL = Android.Opengl.GLES20.GlGetError(); ALL20 errGenericGL = GL20.GetError(); if (errAndroidGL != Android.Opengl.GLES20.GlNoError || errGenericGL != ALL20.NoError) Console.WriteLine(string.Format("OpenGL ES 2.0:\n\tAndroid error: {0,10:X}\n\tGeneric error: {1, 10:X}", errAndroidGL, errGenericGL)); } else { GL20.GenTextures(1, ref _name); GL20.BindTexture(ALL20.Texture2D, _name); GL20.TexParameter(ALL20.Texture2D, ALL20.TextureMinFilter, (int)filter); GL20.TexParameter(ALL20.Texture2D, ALL20.TextureMagFilter, (int)filter); Android.Opengl.GLUtils.TexImage2D((int)ALL20.Texture2D, 0, imagePadded, 0); } } _maxS = _size.Width / (float)_width; _maxT = _size.Height / (float)_height; }
public void InitWithBitmap(Bitmap imageSource, ALL11 filter, ALL11 wrap) { //TODO: Android.Opengl.GLUtils.GetInternalFormat() try { _format = SurfaceFormat.Color; if (imageSource.HasAlpha) _format = SurfaceFormat.Color; if (GraphicsDevice.OpenGLESVersion == OpenTK.Graphics.GLContextVersion.Gles2_0) { _width = imageSource.Width; _height = imageSource.Height; // There are rules for npot textures that we must abide by (wrap = ClampToEdge and filter = Nearest or Linear) if (!MathHelper.IsPowerOfTwo(_width) || !MathHelper.IsPowerOfTwo(_height)) { filter = ALL11.Linear; wrap = ALL11.ClampToEdge; } } else { //scale up bitmap to be power of 2 dimensions but dont exceed 1024x1024. //Note: may not have to do this with OpenGL 2+ _width = (int)Math.Pow(2, Math.Min(10, Math.Ceiling(Math.Log10(imageSource.Width) / Math.Log10(2)))); _height = (int)Math.Pow(2, Math.Min(10, Math.Ceiling(Math.Log10(imageSource.Height) / Math.Log10(2)))); } _size.Width = _width; _size.Height = _height; if (GraphicsDevice.OpenGLESVersion == OpenTK.Graphics.GLContextVersion.Gles2_0) { GL20.GenTextures(1, ref _name); } else { GL11.GenTextures(1, ref _name); } if (_name == 0) { _originalBitmap = imageSource; _originalFilter = filter; _originalWrap = wrap; PrimaryThreadLoader.AddToList(this); _textureCreated = false; } else { _originalBitmap = null; _textureCreated = true; using ( Bitmap imagePadded = Bitmap.CreateBitmap(_width, _height, Bitmap.Config.Argb8888) ) { using(Canvas can = new Canvas(imagePadded)) { can.DrawARGB(0, 0, 0, 0); if(AndroidCompatibility.ScaleImageToPowerOf2) can.DrawBitmap(imageSource, new Rect(0, 0, imageSource.Width, imageSource.Height), new Rect(0, 0, _width, _height), null); //Scale to texture else can.DrawBitmap(imageSource, 0, 0, null); if (GraphicsDevice.OpenGLESVersion == OpenTK.Graphics.GLContextVersion.Gles2_0) { GL20.BindTexture(ALL20.Texture2D, _name); GL20.TexParameter(ALL20.Texture2D, ALL20.TextureMinFilter, (int)filter); GL20.TexParameter(ALL20.Texture2D, ALL20.TextureMagFilter, (int)filter); GL20.TexParameter(ALL20.Texture2D, ALL20.TextureWrapS, (int)wrap); GL20.TexParameter(ALL20.Texture2D, ALL20.TextureWrapT, (int)wrap); Android.Opengl.GLUtils.TexImage2D((int)ALL20.Texture2D, 0, imagePadded, 0); // error checking //int errAndroidGL = Android.Opengl.GLES20.GlGetError(); //ALL20 errGenericGL = GL20.GetError(); //if (errAndroidGL != Android.Opengl.GLES20.GlNoError || errGenericGL != ALL20.NoError) // Console.WriteLine(string.Format("OpenGL ES 2.0:\n\tAndroid error: {0,10:X}\n\tGeneric error: {1, 10:X}", errAndroidGL, errGenericGL)); } else { GL11.BindTexture(ALL11.Texture2D, _name); GL11.TexParameter(ALL11.Texture2D, ALL11.TextureMinFilter, (int)filter); GL11.TexParameter(ALL11.Texture2D, ALL11.TextureMagFilter, (int)filter); GL11.TexParameter(ALL11.Texture2D, ALL11.TextureWrapS, (int)wrap); GL11.TexParameter(ALL11.Texture2D, ALL11.TextureWrapT, (int)wrap); GL11.TexParameter(ALL11.Texture2D, ALL11.GenerateMipmap, 1); Android.Opengl.GLUtils.TexImage2D((int)ALL11.Texture2D, 0, imagePadded, 0); } } } } } finally { if (_originalBitmap != imageSource) { // free bitmap imageSource.Dispose(); } } _maxS = _size.Width / (float)_width; _maxT = _size.Height / (float)_height; }
private static Bitmap GetRoundedCornerBitmap(Bitmap bitmap, int roundPixels, Rect srcRect, Rect destRect, int width, int height) { Bitmap output = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888); Canvas canvas = new Canvas(output); Paint paint = new Paint(); RectF destRectF = new RectF(destRect); paint.AntiAlias = true; canvas.DrawARGB(0, 0, 0, 0); paint.Color = Color.Argb(0xFF, 0x00, 0x00, 0x00); canvas.DrawRoundRect(destRectF, roundPixels, roundPixels, paint); paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcIn)); canvas.DrawBitmap(bitmap, srcRect, destRectF, paint); return output; }
private static Bitmap MakeImageRound (Bitmap bitmap) { Bitmap output = Bitmap.CreateBitmap (bitmap.Width, bitmap.Height, Bitmap.Config.Argb8888); var canvas = new Canvas (output); var rect = new Rect (0, 0, bitmap.Width, bitmap.Height); var rectF = new RectF (rect); var paint = new Paint (); paint.AntiAlias = true; paint.Color = Color.Black; canvas.DrawARGB (0, 0, 0, 0); canvas.DrawRoundRect (rectF, bitmap.Width, bitmap.Height, paint); paint.SetXfermode (new PorterDuffXfermode (PorterDuff.Mode.SrcIn)); canvas.DrawBitmap (bitmap, rect, rect, paint); return output; }
protected override void OnCreate(Bundle bundle) { //Set Display/Interactions/Counter Vars base.OnCreate (bundle); SetContentView (Resource.Layout.Main); Button clear = FindViewById<Button> (Resource.Id.ClearButton); Button save = FindViewById<Button> (Resource.Id.SaveButton); int CountX = -7, CountY = -7; var IV = FindViewById<ImageView> (Resource.Id.DisplayView); float x1=0, y1=0, x2=0, y2=0; Bitmap BM = Bitmap.CreateBitmap (760, 800, Bitmap.Config.Argb8888); //Set up Paint var P = new Paint(); P.Color = Color.White; P.StrokeWidth = 15; Canvas C = new Canvas(BM); //Draw on Bitmap When Touched IV.Touch += (s, e) => { if (e.Event.Action == MotionEventActions.Down) { x1 = e.Event.GetX(); y1 = e.Event.GetY(); x2 = e.Event.GetX(); y2 = e.Event.GetY(); Console.WriteLine ("Touched at X:"+x1+" Y:"+y1); BM.SetPixel ((int)x1, (int)y1, Color.White); IV.SetImageBitmap (BM); } if (e.Event.Action == MotionEventActions.Move) { x1 = e.Event.GetX(); y1 = e.Event.GetY(); while(CountY<=7){ while(CountX<=7){ BM.SetPixel ((int)x1+CountX, (int)y1+CountY, Color.White); CountX++; } CountX=-7; CountY++; } Console.WriteLine ("Touched at X:"+x1+" Y:"+y1); C.DrawLine(x1, y1, x2, y2, P); IV.SetImageBitmap(BM); x2 = x1; y2 = y1; } }; //Clear clear.Click += delegate { C.DrawARGB(255,0,0,0); IV.SetImageBitmap (BM); }; //Save save.Click += delegate { ExportBitmapAsPNG(BM); }; }
void OnZoomThumbnailClicked (object sender, EventArgs e) { View v = (View)sender; // Create a thumbnail animation. We are going to build our thumbnail // just from the view that was pressed. We make sure the view is // not selected, because by the time the animation starts we will // have finished with the selection of the tap. v.DrawingCacheEnabled = true; v.Pressed = false; v.RefreshDrawableState (); Bitmap bm = v.DrawingCache; Canvas c = new Canvas (bm); c.DrawARGB (255, 255, 0, 0); ActivityOptions opts = ActivityOptions.MakeThumbnailScaleUpAnimation (v, bm, 0, 0); // Request the activity be started, using the custom animation options. StartActivity (new Intent (this, typeof (AlertDialogSamples)), opts.ToBundle ()); v.DrawingCacheEnabled = false; }
public void InitWithBitmap(Bitmap imageSource, All filter) { //TODO: Android.Opengl.GLUtils.GetInternalFormat() _format = SurfaceFormat.Color; if(imageSource.HasAlpha) _format = SurfaceFormat.Color; // scale up bitmap to be power of 2 dimensions but dont exceed 1024x1024. // Note: may not have to do this with OpenGL 2+ _width = (int)Math.Pow(2, Math.Min(10,Math.Ceiling(Math.Log10(imageSource.Width) / Math.Log10(2)))); _height = (int)Math.Pow(2, Math.Min(10,Math.Ceiling(Math.Log10(imageSource.Height) / Math.Log10(2)))); _size.Width =_width; _size.Height = _height; using (Bitmap imagePadded = Bitmap.CreateBitmap(_width, _height, Bitmap.Config.Argb8888)) { Canvas can = new Canvas(imagePadded); can.DrawARGB(0, 0, 0, 0); can.DrawBitmap(imageSource, 0, 0, null); GL.GenTextures(1, ref _name); GL.BindTexture(All.Texture2D, _name); GL.TexParameter(All.Texture2D, All.TextureMinFilter, (int)filter); GL.TexParameter(All.Texture2D, All.TextureMagFilter, (int)filter); Android.Opengl.GLUtils.TexImage2D((int)All.Texture2D, 0, imagePadded, 0); } _maxS = _size.Width / (float)_width; _maxT = _size.Height / (float)_height; }
public static Bitmap getCircleBitmap(Context context, Bitmap input, int w , int h) { //CREAMOS UN BITMAP TEMP CON LAS DIMENSIONES QUE LE PASAMOS Bitmap output = Bitmap.CreateBitmap(w, h, Bitmap.Config.Argb8888); //CRAMOS UN CANVAS EL CUAL NOS PERMITIRA DIBUJAR EL CIRCULO QUE NOS SERVIRA COMO MASCARA Y LE ASIGNAMOS EL BITMAP Canvas canvas = new Canvas(output); canvas.DrawARGB(0, 255, 0, 0); //DEFINIMOS UNA BROCHA YA QUE ES NECESARIA PARA DIBUJAR EL CIRCULO Paint paint = new Paint(); paint.AntiAlias=true; //string color = "#ff0002"; //paint.Color=Color.ParseColor (color); //DIBUJAMOS EL CIRCULO DENTRO DEL CANVAS canvas.DrawCircle(canvas.Height /2,canvas.Height /2,canvas.Height /2, paint); //LE APLICAMOS UN FILTRO A NUESTRA BROCHA PARA QUE CUANDO SE CONBINEN LOS ELEMENTOS DE LA IMAGEN ORIGINAL CON LA DIBUJADA EN EL CANVAS //ESTA APLIQUE EL RECORTE DE LA MASCARA paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcIn)); //LE ASIGNAMOS AL FONDO DE NUESTRO CANVAS LA IMAGEN QUE QUEREMOS QUE SE APLIQUE LA MASCARA, EN ESTE CASO ES LA IMAGEN //QUE SE LE ASIGNO AL CONTROL canvas.DrawBitmap(input, 0,0, paint); //REGRESAMOS LA IMAGEN YA RECOTADA return output; }
internal CCTexture2D CreateTextSprite(string text, CCFontDefinition textDefinition) { if (string.IsNullOrEmpty(text)) return new CCTexture2D(); int imageWidth; int imageHeight; var textDef = textDefinition; var contentScaleFactorWidth = CCLabel.DefaultTexelToContentSizeRatios.Width; var contentScaleFactorHeight = CCLabel.DefaultTexelToContentSizeRatios.Height; textDef.FontSize *= contentScaleFactorWidth; textDef.Dimensions.Width *= contentScaleFactorWidth; textDef.Dimensions.Height *= contentScaleFactorHeight; bool hasPremultipliedAlpha; var display = Game.Activity.WindowManager.DefaultDisplay; var metrics = new DisplayMetrics(); display.GetMetrics(metrics); // Do not take into account ScaleDensity for now. // var fontScaleFactor = metrics.ScaledDensity; // textDef.FontSize = (int)(textDef.FontSize * fontScaleFactor); // out paint object to hold our drawn text // var paintFlags = new PaintFlags(); // if (textDefinition.isShouldAntialias) // paintFlags = PaintFlags.AntiAlias | PaintFlags.SubpixelText; var textPaint = new TextPaint(); textPaint.Color = Android.Graphics.Color.White; textPaint.TextAlign = Paint.Align.Left; textPaint.AntiAlias = textDefinition.isShouldAntialias; textPaint.TextSize = textDef.FontSize; var fontName = textDef.FontName; var ext = System.IO.Path.GetExtension(fontName); if (!String.IsNullOrEmpty(ext) && ext.ToLower() == ".ttf") { CCContentManager.SharedContentManager.GetAssetStreamAsBytes(fontName, out fontName); var activity = Game.Activity; try { var typeface = Typeface.CreateFromAsset(activity.Assets, fontName); textPaint.SetTypeface(typeface); } catch (Exception) { textPaint.SetTypeface(Typeface.Create(fontName, TypefaceStyle.Normal)); } } else { textPaint.SetTypeface(Typeface.Create(fontName, TypefaceStyle.Normal)); } // color var foregroundColor = Android.Graphics.Color.White; textPaint.Color = foregroundColor; // alignment var horizontalAlignment = textDef.Alignment; var verticleAlignement = textDef.LineAlignment; var textAlign = (CCTextAlignment.Right == horizontalAlignment) ? Layout.Alignment.AlignOpposite : (CCTextAlignment.Center == horizontalAlignment) ? Layout.Alignment.AlignCenter : Layout.Alignment.AlignNormal; // LineBreak // TODO: Find a way to specify the type of line breaking if possible. var dimensions = new CCSize(textDef.Dimensions.Width, textDef.Dimensions.Height); var layoutAvailable = true; if (dimensions.Width <= 0) { dimensions.Width = 8388608; layoutAvailable = false; } if (dimensions.Height <= 0) { dimensions.Height = 8388608; layoutAvailable = false; } // Get bounding rectangle - we need its attribute and method values var layout = new StaticLayout(text, textPaint, (int)dimensions.Width, textAlign, 1.0f, 0.0f, false); var boundingRect = new Rect(); var lineBounds = new Rect(); // Loop through all the lines so we can find our drawing offsets var lineCount = layout.LineCount; // early out if something went wrong somewhere and nothing is to be drawn if (lineCount == 0) return new CCTexture2D(); for (int lc = 0; lc < lineCount; lc++) { layout.GetLineBounds(lc, lineBounds); var max = (int)Math.Ceiling(layout.GetLineMax(lc)); if (boundingRect.Right < max) boundingRect.Right = max; boundingRect.Bottom = lineBounds.Bottom; } if (!layoutAvailable) { if (dimensions.Width == 8388608) { dimensions.Width = boundingRect.Right; } if (dimensions.Height == 8388608) { dimensions.Height = boundingRect.Bottom; } } imageWidth = (int)dimensions.Width; imageHeight = (int)dimensions.Height; // Recreate our layout based on calculated dimensions so that we can draw the text correctly // in our image when Alignment is not Left. if (textAlign != Layout.Alignment.AlignNormal) { layout = new StaticLayout(text, textPaint, (int)dimensions.Width, textAlign, 1.0f, 0.0f, false); } // Line alignment var yOffset = (CCVerticalTextAlignment.Bottom == verticleAlignement || boundingRect.Bottom >= dimensions.Height) ? dimensions.Height - boundingRect.Bottom // align to bottom : (CCVerticalTextAlignment.Top == verticleAlignement) ? 0 // align to top : (imageHeight - boundingRect.Bottom) * 0.5f; // align to center try { // Create our platform dependant image to be drawn to. using (Bitmap textureBitmap = Bitmap.CreateBitmap(imageWidth, imageHeight, Bitmap.Config.Argb8888)) { using (Canvas drawingCanvas = new Canvas(textureBitmap)) { drawingCanvas.DrawARGB(0, 255, 255, 255); // Set our vertical alignment drawingCanvas.Translate(0, yOffset); // Now draw the text using our layout layout.Draw(drawingCanvas); // Create a pixel array int[] pixels = new int[imageWidth * imageHeight]; // Now lets get our pixels. // We use CopyPixelsToBuffer so that it is in Premultiplied Alpha already. // Using Bitmap.GetPixels return non Premultiplied Alpha which causes blending problems Java.Nio.IntBuffer byteBuffer = Java.Nio.IntBuffer.Allocate(imageWidth * imageHeight); textureBitmap.CopyPixelsToBuffer(byteBuffer); if (byteBuffer.HasArray) { byteBuffer.Rewind(); byteBuffer.Get(pixels, 0, pixels.Length); } // Make sure we recycle - Let's keep it green textureBitmap.Recycle(); // Here we create our Texture and then set our pixel data. var texture = new CCTexture2D(imageWidth, imageHeight); texture.XNATexture.SetData<int>(pixels); return texture; } } } catch (Exception exc) { CCLog.Log ("CCLabel Android: Error creating native label:{0}\n{1}", exc.Message, exc.StackTrace); return new CCTexture2D(); } }
public static Bitmap GetRoundedCornerBitmap (Bitmap bitmap, int cornerRadius) { Bitmap output = Bitmap.CreateBitmap (bitmap.Width, bitmap.Height, Bitmap.Config.Argb8888); Canvas canvas = new Canvas(output); Paint paint = new Paint(); Rect rect = new Rect(0, 0, bitmap.Width, bitmap.Height); RectF rectF = new RectF(rect); float roundPx = cornerRadius; paint.AntiAlias = true; canvas.DrawARGB(0, 0, 0, 0); paint.SetColorFilter (new ColorFilter ()); canvas.DrawRoundRect(rectF, roundPx, roundPx, paint); paint.SetXfermode (new PorterDuffXfermode (PorterDuff.Mode.SrcIn)); canvas.DrawBitmap (bitmap, rect, rect, paint); return output; }
private Bitmap GetCircleBitmap(int radius) { var output = Bitmap.CreateBitmap (_originBitmap.Width, _originBitmap.Height, Bitmap.Config.Argb8888); using (var canvas = new Canvas (output)) using (var paint = new Paint (PaintFlags.AntiAlias)) using (var rect = new Rect ((int)(_x - radius), (int)(_y - radius), (int)(_x + radius), (int)(_y + radius))) { canvas.DrawARGB (0, 0, 0, 0); canvas.DrawCircle (_x, _y, radius, paint); paint.SetXfermode (new PorterDuffXfermode (PorterDuff.Mode.SrcIn)); canvas.DrawBitmap (_originBitmap, rect, rect, paint); return output; } }
/// <summary> /// Reshapes the image. /// </summary> /// <param name="sourceBitmap">The source bitmap.</param> private void ReshapeImage(Bitmap sourceBitmap) { if (sourceBitmap == null) return; using (var sourceRect = GetScaledRect(sourceBitmap.Height, sourceBitmap.Width)) using (var rect = this.GetTargetRect(sourceBitmap.Height, sourceBitmap.Width)) using (var output = Bitmap.CreateBitmap(rect.Width(), rect.Height(), Bitmap.Config.Argb8888)) using (var canvas = new Canvas(output)) using (var paint = new Paint()) using (var rectF = new RectF(rect)) { var roundRx = rect.Width() / 2; var roundRy = rect.Height() / 2; paint.AntiAlias = true; canvas.DrawARGB(0, 0, 0, 0); paint.Color = Android.Graphics.Color.ParseColor("#ff424242"); canvas.DrawRoundRect(rectF, roundRx, roundRy, paint); paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcIn)); canvas.DrawBitmap(sourceBitmap, sourceRect, rect, paint); //this.DrawBorder(canvas, rect.Width(), rect.Height()); this.Control.SetImageBitmap(output); // Forces the internal method of InvalidateMeasure to be called. this.Element.WidthRequest = this.Element.WidthRequest; } }
/// <summary> /// Gets the oval bitmap. /// </summary> /// <returns>The oval bitmap.</returns> /// <param name="bitmap">Bitmap.</param> private Bitmap GetOvalBitmap(Bitmap bitmap) { Bitmap output = null; if(bitmap != null) { output = Bitmap.CreateBitmap(bitmap.Width, bitmap.Height, Bitmap.Config.Argb8888); Canvas canvas = new Canvas(output); Paint paint = new Paint(); Rect rect = new Rect(0, 0, bitmap.Width, bitmap.Height); RectF rectF = new RectF(rect); paint.AntiAlias = true; canvas.DrawARGB(0, 0, 0, 0); paint.Color = Color.Red; canvas.DrawOval(rectF, paint); paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcIn)); canvas.DrawBitmap(bitmap, rect, rect, paint); } return output; }