我只是在玩 Xamarin Forms 和 SkiaSharp。我用 SkiaCanvas 创建了一个简单的 Xamarin 表单 ContentView。我的PaintSurface如下绘制一个简单的绿色圆圈:
private void canvas_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
var view = (SKCanvasView)sender;
var canvas = e.Surface.Canvas;
using(var paint = new SKPaint())
{
paint.Color = Colours.XamarinGreen;
paint.Style = SKPaintStyle.Fill;
paint.StrokeWidth = 1;
paint.IsAntialias = true;
canvas.DrawCircle((float)view.Width / 2.0f, (float)view.Height / 2.0f, (float)view.Height / 2.0f, paint);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的测试应用程序中,当我通过拖动窗口的一角来调整窗口大小时,圆圈会闪烁。我想知道在 Xamarin.Forms 应用程序中使用 SkiaSharp 绘制图形时是否有一种简单的方法可以启用双缓冲?
如何绘制旋转文本SkiaSharp
.
目前我正在旋转SKCanvas
,绘制文本然后将其旋转回来.但我想可能有更有效的方法来做到这一点.
canvas.RotateDegrees(45, 20, 20);
canvas.DrawText("Text", 20, 20, paint);
canvas.RotateDegrees(-45, 20, 20);
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 SkiaSharp 将 SVG 文件转换为 PNG。我知道有很多选择,但我想评估它的 SVG 负载支持。
这可能吗?我试过这个:
var svg = new SKSvg(new SKSize(200, 200));
svg.Load("image.svg");
var bitmap = new SKBitmap((int)svg.CanvasSize.Width, (int)svg.CanvasSize.Height);
var canvas = new SKCanvas(bitmap);
canvas.DrawPicture(svg.Picture);
canvas.Flush();
canvas.Save();
using (var image = SKImage.FromBitmap(bitmap))
using (var data = image.Encode(SKImageEncodeFormat.Png, 80))
{
// save the data to a stream
using (var stream = File.OpenWrite("image.png"))
{
data.SaveTo(stream);
}
}
Run Code Online (Sandbox Code Playgroud)
但我只是得到一个空图像。
假设我有svg文件,如下所示:
"<svg height='300' width='300'><rect class='rect us' id='12212' x='34' y='54' width='20' height='20'/><rect class='rect us' id='12213' x='64' y='54' width='20' height='20'/></svg>
Run Code Online (Sandbox Code Playgroud)
是否可以按给定的点命中SVGElement测试并获得该元素的ID?如果可以,我该如何完成这项任务?我做了一些谷歌搜索,却找不到任何东西。
在 System.Drawing 中,我们从 Image 对象中检索 PixelFormat,但 SkiaSharp.SkImage 不提供 API 来查找解码图像的 PixelFormat。是否有任何其他解决方法来查找解码图像的 PixelFormat 以及我们如何创建具有 PixelFormat 值的 Image 作为 System.Drawing.Image
SKBitmap.Bytes 是只读的,关于如何 Marshal.Copy 字节数组到 SKBitmap 有什么建议吗?我正在使用下面的代码片段,但它不起作用。
代码片段:
SKBitmap bitmap = new SKBitmap((int)Width, (int)Height);
bitmap.LockPixels();
byte[] array = new byte[bitmap.RowBytes * bitmap.Height];
for (int i = 0; i < pixelArray.Length; i++)
{
SKColor color = new SKColor((uint)pixelArray[i]);
int num = i % (int)Width;
int num2 = i / (int)Width;
array[bitmap.RowBytes * num2 + 4 * num] = color.Blue;
array[bitmap.RowBytes * num2 + 4 * num + 1] = color.Green;
array[bitmap.RowBytes * num2 + 4 * num + 2] = color.Red;
array[bitmap.RowBytes …
Run Code Online (Sandbox Code Playgroud) 我正在寻找调整 SKImage 大小的最快方法。不幸的是我发现了一些例子。
我问这个问题是因为如果我并行执行这些函数(在我的例子中大约有 60 个线程),每个缩放函数的执行时间会增加多达二十倍。
我尝试了以下方法,性能看起来非常相似,有什么更好的吗?
方法一:
SKImage src = (...);
SKImageInfo info = new SKImageInfo(width, height, SKColorType.Bgra8888);
SKImage output = SKImage.Create(info);
src.ScalePixels(output.PeekPixels(), SKFilterQuality.None);
Run Code Online (Sandbox Code Playgroud)
方法二:
SKImage src = (...);
SKImage output;
float resizeFactorX = (float)width / (float)Src.Width;
float resizeFactorY = (float)height / (float)Src.Height;
using (SKSurface surface = SKSurface.Create((int)(Src.Width *
resizeFactorX), (int)(Src.Height * resizeFactorY),
SKColorType.Bgra8888, SKAlphaType.Opaque))
{
surface.Canvas.SetMatrix(SKMatrix.MakeScale(resizeFactorX, resizeFactorY));
surface.Canvas.DrawImage(Src, 0, 0);
surface.Canvas.Flush();
output = surface.Snapshot();
}
Run Code Online (Sandbox Code Playgroud) 在system.drawing中有一个pixelformat,我们在创建BITMAP时添加了像素格式。请参考下面的代码。
Bitmap image = new Bitmap((int)width, (int)height, PixelFormat.Format1bppIndexed);
Run Code Online (Sandbox Code Playgroud)
由于 ASP.NET Core 不支持系统绘图,我们使用了skiasharp。在其中我们找不到位图的像素格式。请为以下像素提供skiasharp 中的等效像素格式。
PixelFormat.Format1bppIndexed
PixelFormat.Format32bppArgb
PixelFormat.Format8bppIndexed
PixelFormat.Format24bppRgb
PixelFormat.Format4bppIndexed
Run Code Online (Sandbox Code Playgroud)
任何人都可以提供在skiasharp 中查找等效像素格式类型的建议。
谢谢,莫汉小号
我尝试按照https://skia.org/user/build 中的建议在 Windows 上编译skia
1> bin/gn gen out/Static --args='is_official_build=true'
2> ninja -C out/Static
构建无法找到 libjpeg.h、libpng.h
然后我在 gn args 中添加了以下行
extra_cflags = ["-I ../../third_party/externals/libpng/", "-I ../../third_party/externals/libjpeg-turbo/"]
Run Code Online (Sandbox Code Playgroud)
它仍然无法找到两个标题。
但关键是我不应该传递包含路径,因为如果 JPEG/PNG 是skia 的核心部分,它应该在内部处理。
如何在 Windows 上构建它?
当我从 GPS 传感器读取数据时,有轻微的延迟。你不会得到像 0,1 0,2 0,3 0,4 0,5 等这样的值,但它们会像 1 一样突然变成 5 或 9 或 12。在这种情况下,针会来回跳动。有人知道如何使针顺利移动吗?我想需要某种延迟?
类似的东西,取自另一个控件:
async void animateProgress(int progress)
{
sweepAngle = 1;
// Looping at data interval of 5
for (int i = 0; i < progress; i=i+5)
{
sweepAngle = i;
await Task.Delay(3);
}
}
Run Code Online (Sandbox Code Playgroud)
但是我有点困惑如何实现它。
以下是在画布上绘制针的代码:
private void OnDrawNeedle()
{
using (var needlePath = new SKPath())
{
//first set up needle pointing towards 0 degrees (or 6 o'clock)
var widthOffset = ScaleToSize(NeedleWidth / 2.0f);
var …
Run Code Online (Sandbox Code Playgroud)