Notice the use of the Clear method to make the background of rotatedBitmap a light pink. (The Translate and RotateDegrees methods are called in the opposite order than how they are applied.) The transforms used to draw the original bitmap on the rotated bitmap move the original bitmap center to the origin, then rotate it by the specified number of degrees, and then translate that center to the center of the rotated bitmap. The new width and height are based on absolute values of sines and cosines of the original widths and heights. The ValueChanged handler of the Slider performs the operations that create a new rotatedBitmap based on the rotation angle. Using (SKCanvas canvas = new SKCanvas(rotatedBitmap))Ĭanvas.Translate(rotatedWidth / 2, rotatedHeight / 2) Ĭanvas.Translate(-originalWidth / 2, -originalHeight / 2) Ĭanvas.DrawBitmap(originalBitmap, new SKPoint()) RotatedBitmap = new SKBitmap(rotatedWidth, rotatedHeight) Int rotatedHeight = (int)(cosine * originalHeight + sine * originalWidth) Int rotatedWidth = (int)(cosine * originalWidth + sine * originalHeight) Int originalHeight = originalBitmap.Height Int originalWidth = originalBitmap.Width Void OnSliderValueChanged(object sender, ValueChangedEventArgs args)įloat sine = (float)Math.Abs(Math.Sin(radians)) įloat cosine = (float)Math.Abs(Math.Cos(radians)) Static readonly SKBitmap originalBitmap =īitmapExtensions.LoadBitmapResource(typeof(BitmapRotatorPage),Ĭanvas.DrawBitmap(rotatedBitmap, info.Rect, BitmapStretch.Uniform) The bitmap displayed in the PaintSurface handler is rotatedBitmap, which is initially set to originalBitmap: public partial class BitmapRotatorPage : ContentPage The code-behind file loads a bitmap resource and saves it as a static read-only field named originalBitmap. Void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) SKCanvasView canvasView = new SKCanvasView() ĬanvasView.PaintSurface += OnCanvasViewPaintSurface Using (SKCanvas bitmapCanvas = new SKCanvas(helloBitmap))īitmapCanvas.DrawText(TEXT, 0, -bounds.Top, textPaint) HelloBitmap = new SKBitmap((int)bounds.Right, The constructor concludes by creating an SKCanvasView object to display the bitmap: public partial class HelloBitmapPage : ContentPage It's always a good idea to call Clear with a new bitmap because a newly created bitmap might contain random data. It then creates an SKCanvas object based on that bitmap, calls Clear, and then calls DrawText. It determines the dimensions of a text string and creates a bitmap with those dimensions. The constructor of the HelloBitmapPage begins by creating an SKPaint object for displaying text. The Hello Bitmap page in the SkiaSharpFormsDemos application writes the text "Hello, Bitmap!" on a bitmap and then displays that bitmap multiple times. At a later time, the program can create a new SKCanvas object based on that same bitmap, and draw on it some more. For this reason, the SKCanvas constructor is generally called in a using statement: using (SKCanvas canvas = new SKCanvas(bitmap)) When you're finished drawing on the bitmap, you can dispose of the SKCanvas object. To draw on a bitmap, you create an SKCanvas object using the SKCanvas constructor: SKCanvas canvas = new SKCanvas(bitmap) To draw on a video display, you obtain an SKCanvas object from the PaintSurface event arguments. Drawing on the bitmapĭrawing on the surface of a bitmap is the same as drawing on a video display. The second approach is discussed in the article Accessing SkiaSharp Bitmap Pixels. This article demonstrates the first approach: Draw on the bitmap using standard Canvas drawing methods.You generally do this in one of two ways: This constructor creates a full-color bitmap with four bytes per pixel: one byte each for the red, green, blue, and alpha (opacity) components.Īfter you've created a new bitmap, you need to get something on the surface of the bitmap. The width and height parameters are integers and specify the pixel dimensions of the bitmap. The simplest approach involves one of the constructors of SKBitmap: SKBitmap bitmap = new SKBitmap(width, height) It's also possible to create new bitmaps within your application. You've seen how an application can load bitmaps from the Web, from application resources, and from the user's photo library.
0 Comments
Leave a Reply. |