Elvileg jó, meg a mentés is.
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
using SkiaSharp;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WorkFlowCheck.MAUI.Helper
|
||||
{
|
||||
public static class ImageHelper
|
||||
{
|
||||
public static string ResizeAndSaveImage(byte[] imageBytes, int id, int targetWidth = 50)
|
||||
{
|
||||
// Átméretezett fájl elérési útja
|
||||
var filename = Path.Combine(FileSystem.CacheDirectory, $"wfc_{id}_photo.jpg");
|
||||
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(filename);
|
||||
// opcionálisan logolhatsz
|
||||
// Console.WriteLine($"Törölve: {filename}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//Console.WriteLine($"Hiba a fájl törlésekor: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// Eredeti kép betöltése
|
||||
using var inputStream = new MemoryStream(imageBytes);
|
||||
using var original = SKBitmap.Decode(inputStream);
|
||||
|
||||
if (original == null)
|
||||
throw new InvalidOperationException("A kép nem olvasható vagy hibás.");
|
||||
|
||||
// Új szélesség és magasság arányosan
|
||||
|
||||
int targetHeight = (int)(original.Height * ((double)targetWidth / original.Width));
|
||||
|
||||
// Új üres bitmap létrehozása
|
||||
using var resizedSurface = SKSurface.Create(new SKImageInfo(targetWidth, targetHeight));
|
||||
using var canvas = resizedSurface.Canvas;
|
||||
|
||||
canvas.Clear(SKColors.White); // választható háttérszín
|
||||
|
||||
var srcRect = new SKRect(0, 0, original.Width, original.Height);
|
||||
var destRect = new SKRect(0, 0, targetWidth, targetHeight);
|
||||
canvas.DrawBitmap(original, srcRect, destRect);
|
||||
|
||||
using var resizedImage = resizedSurface.Snapshot();
|
||||
using var imageData = resizedImage.Encode(SKEncodedImageFormat.Jpeg, 90);
|
||||
|
||||
// Mentés fájlba
|
||||
using var fileStream = File.OpenWrite(filename);
|
||||
imageData.SaveTo(fileStream);
|
||||
|
||||
return filename;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user