timelapsenow/Timelapse/PictureBox.cs

80 lines
1.8 KiB
C#
Raw Normal View History

2022-05-20 02:06:35 +00:00
using Eto.Drawing;
using Eto.Forms;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using TimelapseApi;
namespace Timelapse.Desktop;
public class PictureBox : Panel
{
LockObj lockObj=LockObj.Create();
ImageView view =new ImageView();
public PictureBox()
{
this.Content=view;
this.SizeChanged+=(sender,e)=>{
Resize();
};
}
private void SetImage(Stream strm)
{
if(view.Image != null && !view.Image.IsDisposed)
{
view.Image.Dispose();
}
view.Image = new Bitmap(strm);
}
private Image<Rgb24>? img=null;
public Image<Rgb24>? Image {get {return img;} set{ if(value !=null){SetImage(value);}}}
public void SetImage(Image<Rgb24> image)
{
lockObj.Lock(()=>{
img=image;
if(img !=null)
{
using(var img2=img.Clone())
{
img2.Mutate((e)=>{
e.Resize(Width <= 0 ? 640 : Width,Height <= 0 ? 480 : Height,false);
});
MemoryStream strm=new MemoryStream();
img2.SaveAsBmp(strm);
strm.Position=0;
SetImage(strm);
}
}
});
}
public void Resize()
{
lockObj.Lock(()=>{
try{
if(img !=null)
{
using(var img2=img.Clone())
{
img2.Mutate((e)=>{
e.Resize(Width <= 0 ? 640 : Width,Height <= 0 ? 480 : Height,false);
});
MemoryStream strm=new MemoryStream();
img2.SaveAsBmp(strm);
strm.Position=0;
SetImage(strm);
}
}
}catch(Exception ex)
{
_=ex;
}
});
}
}