Я работаю над проектом, используя Windows формы и SkiaSharp.
Мне нужно нарисовать несколько фигур и расположить их над изображением. Я не хочу рисовать их прямо на изображении по ряду причин. Я рендерил фигуры, нарисованные на холсте с прозрачным фоном, в растровое изображение, а затем накладываю это растровое изображение на мое изображение. Моя проблема заключается в том, что входное изображение имеет разрешение 299,9994 точек на дюйм (System.Drawing.Bitmap.Hor HorizontalResolution & System.Drawing.Bitmap.VerticalResolution), растровое изображение, нарисованное на холсте, сообщает о разрешении 96 точек на дюйм. Это приводит к тому, что составное изображение не выравнивается должным образом с исходным изображением, а чертеж показан примерно в 3 раза больше исходного изображения.
Я пытаюсь найти, как установить разрешение поверхности или холста, прежде чем рисовать на нем, поэтому сгенерированное растровое изображение имеет то же разрешение, что и оригинал.
Вот простая демонстрация из того, что я пытаюсь сделать:
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using SkiaSharp;
using SkiaSharp.Views.Desktop;
namespace SkiaSharpTest1
{
public partial class Form1 : Form
{
private int width = 600;
private int height = 600;
private Bitmap image1;
private Bitmap image2;
public Form1()
{
InitializeComponent();
}
//let the user select an image and show it in picturebox1
//also assign width and height from the original image
private void btnSelectImage1_Click(object sender, EventArgs e)
{
FileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() != DialogResult.OK) return;
var filename = dialog.FileName;
image1 = new Bitmap(filename);
//EDIT: adding this fixed my immediate issue
//set the original image resolution to the canvas default 96 dpi
image1.SetResolution(96.0f, 96.0f);
pictureBox1.Image = image1;
width = image1.Width;
height = image1.Height;
}
//create a surface the same size as the original image
//create a transparent canvas then draw the shape on it
//then capture as bitmap and display in picturebox2
//TODO: How to set the canvas resolution to match the original image??
private void btnDrawImage2_Click(object sender, EventArgs e)
{
var imageInfo = new SKImageInfo(width, height);
using (var surface = SKSurface.Create(imageInfo))
{
using (var canvas = surface.Canvas)
{
canvas.Clear(SKColors.Transparent);
using (SKPaint paint = new SKPaint())
{
paint.Color = SKColors.Blue;
paint.IsAntialias = true;
paint.StrokeWidth = (int) (width * .005);
paint.Style = SKPaintStyle.Stroke;
canvas.DrawCircle(width / 2, height / 2, height / 2 - 50, paint);
}
}
image2 = SufaceToBitmap(surface);
pictureBox2.Image = image2;
}
}
//helper to get the surface back to a system.drawing.bitmap for display in a picturebox
private static Bitmap SufaceToBitmap(SKSurface surface)
{
using (SKImage image = surface.Snapshot())
{
return image.ToBitmap();
}
}
//composite the two images together
private void btnComposite_Click(object sender, EventArgs e)
{
SKImageInfo imageInfo = new SKImageInfo(width, height);
using (var surface = SKSurface.Create(imageInfo))
{
using (var canvas = surface.Canvas)
{
canvas.Clear(SKColors.Transparent);
using (SKPaint paint = new SKPaint())
{
canvas.DrawBitmap(image1.ToSKBitmap(), 0, 0, paint);
canvas.DrawBitmap(image2.ToSKBitmap(), 0, 0, paint);
}
}
pictureBox3.Image = SufaceToBitmap(surface);
}
}
}
}
Как я могу установить разрешение холста, чтобы оно соответствовало моему исходному изображению до рисования на холсте?
РЕДАКТИРОВАТЬ: я нашел простой fix, который устанавливает разрешение исходного изображения в соответствии с разрешением холста. Я все еще хотел бы знать, как установить разрешение холста или, если это вообще возможно.
Спасибо!