public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
}
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
string image_path = "";
Bitmap originalImage;
private void pictureBox1_Click(object sender, EventArgs e)
{
SelectImage();
}
private void SelectImage()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
if (pictureBox1.Image != null)
pictureBox1.Image.Dispose();
image_path = ofd.FileName;
originalImage = new Bitmap(image_path);
pictureBox1.Invalidate();
ofd.Dispose();
if (target != null)
target.Dispose();
target = null;
}
int modelSize = 512;
string model_path;
SessionOptions options;
InferenceSession onnx_session;
Tensor<float> input_tensor;
List<NamedOnnxValue> input_container;
private void FrmMain_Load(object sender, EventArgs e)
{
toolStripButton3.SelectedIndex = 2;
rect = new Rectangle((pictureBox1.Width - 514)>>1, (pictureBox1.Height - 514) >> 1, 514, 514);
// 创建输出会话,用于输出模型读取信息
options = new SessionOptions();
options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
//设置为CPU上运行
options.AppendExecutionProvider_CPU(0);
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(image_path))
{
MessageBox.Show("请加载图片");
return;
}
if (target==null || pictureBox2.Image==null)
{
MessageBox.Show("请移动红框选择区域");
return;
}
if (target.Height != modelSize && target.Width != modelSize)
{
MessageBox.Show($"请选择图像大小为{modelSize}×{modelSize}");
return;
}
WaitAction(() =>
{
if (pictureBox2.Image != null)
pictureBox2.Image.Dispose();
pictureBox2.Image = new Bitmap(repaired_image.ToMemoryStream());
repaired_image.Dispose();
image.Dispose();
});
}
private void toolStripButton3_SelectedIndexChanged(object sender, EventArgs e)
{
var startupPath = Application.StartupPath;
model_path = startupPath + $"\\model\\{toolStripButton3.SelectedItem.ToString()}.onnx";
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
SelectImage();
}
private Rectangle rect;
private bool dragging = false;
private System.Drawing.Point dragStart = System.Drawing.Point.Empty;
private System.Drawing.Point downStart= System.Drawing.Point.Empty;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (rect.Contains(e.Location))
{
dragging = true;
dragStart = new System.Drawing.Point(e.X - rect.X, e.Y - rect.Y);
}
else
{
downStart = e.Location;
}
}
int width = 512;
int height = 512;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
{
rect.X = e.X - dragStart.X;
rect.Y = e.Y - dragStart.Y;
pictureBox1.Invalidate();
}
else
{
m_ptCanvasX =e.Location.X - downStart.X;
m_ptCanvasY = e.Location.Y - downStart.Y;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (dragging)
{
CreateBmp();
}
dragging = false;
downStart= System.Drawing.Point.Empty;
}
private void CreateBmp()
{
if (originalImage == null) return;
var bmp = pictureBox1.Handle.CaptureControl(pictureBox1.Width, pictureBox1.Height);
if (bmp == null) return;
if (target != null)
target.Dispose();
target = new Bitmap(rect.Width - 2, rect.Height - 2);
using (Graphics graphics = Graphics.FromImage(target))
{
graphics.DrawImage(bmp, new Rectangle(0, 0, target.Width, target.Height),
new Rectangle(rect.X + 2, rect.Y + 2, rect.Width - 2, rect.Height - 2), GraphicsUnit.Pixel);
}
// 将截取的图像显示在另一个PictureBox中
if (pictureBox2.Image != null)
pictureBox2.Image.Dispose();
pictureBox2.Image = (Bitmap)target.Clone();
bmp.Dispose();
}
Bitmap target = null;
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (originalImage != null)
Test111(e, originalImage);
e.Graphics.DrawRectangle(Pens.Red, rect);
}
private float scaleFactor = 1.0f; // 缩放因子
private void toolStripButton5_Click(object sender, EventArgs e)
{
scaleFactor += 0.1f;
pictureBox1.Invalidate();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
scaleFactor -= 0.1f;
if (scaleFactor < 0.0f) scaleFactor = 0.0f;
pictureBox1.Invalidate();
}
int x=0, y=0;
int m_ptCanvasX, m_ptCanvasY;
int dw, dh;
private void toolStripButton6_Click(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(image_path) || pictureBox2.Image == null)
{
MessageBox.Show("请先修复图片");
return;
}
using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
{
folderBrowserDialog.Description = "请选择一个文件夹";
DialogResult dialogResult = folderBrowserDialog.ShowDialog();
if (dialogResult == DialogResult.OK)
{
string selectedFolderPath = folderBrowserDialog.SelectedPath;
string path=Path
.Combine(selectedFolderPath, DateTime.Now.ToString("yyyyMMddHHmmss")+Path.GetExtension(image_path));
pictureBox2.Image.Save(path);
MessageBox.Show("保存成功");
}
}
}
private void Test111(PaintEventArgs e, Bitmap bitmap)
{
BufferedGraphicsContext GraphicsContext = BufferedGraphicsManager.Current;
BufferedGraphics myBuffer = GraphicsContext.Allocate(e.Graphics, e.ClipRectangle);
Graphics g = myBuffer.Graphics;
int destWidth = pictureBox1.Width;
int destHeight = pictureBox1.Height;
int srcWidth = bitmap.Width;
int srcHeight = bitmap.Height;
float scale = scaleFactor;
int width = (int)(srcWidth * scale);
int height = (int)(srcHeight * scale);
dw = width; dh=height;
x = (destWidth - width) >> 1;
y = (destHeight - height) >> 1;
//g.TranslateTransform(m_ptCanvasX, m_ptCanvasY);
g.ScaleTransform(scaleFactor, scaleFactor);
g.DrawImage(bitmap, x, y, width, height);
myBuffer.Render(e.Graphics);
g.Dispose();
myBuffer.Dispose();
}
private void WaitAction(Action action)
{
using(FrmWait fw=new FrmWait())
{
((Action)(() =>
{
action();
})).BeginInvoke(new AsyncCallback((ar) => (ar.AsyncState as FrmWait).DialogResult = DialogResult.OK), fw);
fw.StartPosition = FormStartPosition.CenterScreen;
fw.ShowDialog();
}
}
}
可根据ROI框手动选择512*512的人脸修复