// ExStart:RenderShapeToGraphics public static string RenderShapeToGraphics(string dataDir, Shape shape) { ShapeRenderer r = shape.GetShapeRenderer(); // Find the size that the shape will be rendered to at the specified scale and resolution. Size shapeSizeInPixels = r.GetSizeInPixels(1.0f, 96.0f); // Rotating the shape may result in clipping as the image canvas is too small. Find the longest side // And make sure that the graphics canvas is large enough to compensate for this. int maxSide = System.Math.Max(shapeSizeInPixels.Width, shapeSizeInPixels.Height); using (Android.Graphics.Bitmap bitmap = Android.Graphics.Bitmap.CreateBitmap((int)(maxSide * 1.25), (int)(maxSide * 1.25), Android.Graphics.Bitmap.Config.Argb8888)) { // Rendering to a graphics object means we can specify settings and transformations to be applied to // The shape that is rendered. In our case we will rotate the rendered shape. using (Android.Graphics.Canvas gr = new Android.Graphics.Canvas(bitmap)) { // Clear the shape with the background color of the document. gr.DrawColor(new Android.Graphics.Color(shape.Document.PageColor.ToArgb())); // Center the rotation using translation method below gr.Translate((float)bitmap.Width / 8, (float)bitmap.Height / 2); // Rotate the image by 45 degrees. gr.Rotate(45); // Undo the translation. gr.Translate(-(float)bitmap.Width / 8, -(float)bitmap.Height / 2); // Render the shape onto the graphics object. r.RenderToSize(gr, 0, 0, shapeSizeInPixels.Width, shapeSizeInPixels.Height); } // Save output to file. using (System.IO.FileStream fs = System.IO.File.Create(dataDir + "/RenderToSize_Out.png")) { bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, fs); } } return("\nShape rendered to graphics successfully.\nFile saved at " + dataDir); }
protected override void OnDraw(Android.Graphics.Canvas canvas) { base.OnDraw(canvas); canvas.DrawColor(Colors.White.ToDroid()); using (var ctx = canvas.XwtContext()) { var p = new ReferencePainter(); p.Font = this.Font; if (true) { p.Figures(ctx, 5, 5); p.Transforms(ctx, 5, 150); p.Texts(ctx, 5, 250); p.PatternsAndImages(ctx, 205, 5); } else { TestDrawing(ctx, 5, 5); } } }
public void SurfaceCreated(ISurfaceHolder holder) { this.holder = holder; //畫筆定義 Android.Graphics.Paint mpaint = new Android.Graphics.Paint(); mpaint.Color = Android.Graphics.Color.Yellow; mpaint.SetStyle(Android.Graphics.Paint.Style.Stroke); mpaint.StrokeWidth = 4f; //畫布 Android.Graphics.Canvas canvas = holder.LockCanvas(); //清除畫布用 canvas.DrawColor(Android.Graphics.Color.Transparent, Android.Graphics.PorterDuff.Mode.Clear); // 畫布長寬 var w = sv_rec.Width; var h = sv_rec.Height; //控制正方形位置 Android.Graphics.Rect r = new Android.Graphics.Rect((int)w / 16, (int)h / 8, (int)w / 16 + 7 * w / 8, (int)h / 8 + h / 2); //canvas.DrawRect(r, mpaint); //正方形 //左下框 canvas.DrawRect(r.Left - 2, r.Bottom, r.Left + 50, r.Bottom + 2, mpaint); canvas.DrawRect(r.Left - 2, r.Bottom - 50, r.Left, r.Bottom, mpaint); //左上框 canvas.DrawRect(r.Left - 2, r.Top - 2, r.Left + 50, r.Top, mpaint); canvas.DrawRect(r.Left - 2, r.Top, r.Left, r.Top + 50, mpaint); //右上框 canvas.DrawRect(r.Right - 50, r.Top - 2, r.Right + 2, r.Top, mpaint); canvas.DrawRect(r.Right, r.Top, r.Right + 2, r.Top + 50, mpaint); //右下框 canvas.DrawRect(r.Right - 50, r.Bottom, r.Right + 2, r.Bottom + 2, mpaint); canvas.DrawRect(r.Right, r.Bottom - 50, r.Right + 2, r.Bottom, mpaint); holder.UnlockCanvasAndPost(canvas); }
void ScreenshotTest(object sender,EventArgs EventArgs) { Android.Graphics.Bitmap b = Android.Graphics.Bitmap.CreateBitmap(RI.TL.Width,RI.TL.Height,Android.Graphics.Bitmap.Config.Argb8888); Android.Graphics.Canvas c = new Android.Graphics.Canvas (b); c.DrawColor (Android.Graphics.Color.White); RI.TL.Draw(c); using (System.IO.FileStream fs = new System.IO.FileStream ( "/sdcard/screen.png", System.IO.FileMode.Create)) { b.Compress (Android.Graphics.Bitmap.CompressFormat.Png, 90, fs);} var uri = Android.Net.Uri.FromFile (new Java.IO.File("/sdcard/screen.png")); var email = new Intent (Android.Content.Intent.ActionSend); email.SetType ("message/rfc822"); email.PutExtra (Android.Content.Intent.ExtraEmail, new string[]{"*****@*****.**"} ); email.PutExtra (Android.Content.Intent.ExtraCc, new string[]{"*****@*****.**"} ); email.PutExtra (Android.Content.Intent.ExtraSubject, "[ModemListe]"); email.PutExtra (Android.Content.Intent.ExtraText, "Hello from Xamarin.Android"); email.PutExtra (Intent.ExtraStream,uri); StartActivity (Intent.CreateChooser(email,"send")); }