Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

renew OpenCvSharp.DebuggerVisualizers #1577

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .cr/personal/FavoritesList/List.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Root Type="DevExpress.CodeRush.Foundation.CodePlaces.Options.FavoritesListContainer">
<Options Language="Neutral">
<Groups />
</Options>
</Root>
137 changes: 109 additions & 28 deletions src/OpenCvSharp.DebuggerVisualizers/ImageViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ public partial class ImageViewer : Form
public ImageViewer()
{
InitializeComponent();
this.MouseWheel += MainForm_MouseWheel;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MouseMove += MainForm_MouseMove;
this.MouseDown += MainForm_MouseDown;

this.MouseUp += MainForm_MouseUp;

this.DoubleBuffered = true;

}

public ImageViewer(MatProxy proxy)
Expand All @@ -36,48 +45,120 @@ private void DisposeBitmap()
{
bitmap?.Dispose();
}


protected override void OnLoad(EventArgs e)
/// <summary>
/// 最小缩放尺寸
/// </summary>
int minZoom = 1;

/// <summary>
/// 缩放尺寸
/// </summary>
int zoom = 1;
/// <summary>
/// 滚动缩放图片,像素级别放大,不模糊
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_MouseWheel(object sender, MouseEventArgs e)
{
base.OnLoad(e);
if (e.Delta > 0)
{
zoom++;
}
else
{ zoom--; }

var ratio = SetClientSize(new System.Drawing.Size(bitmap.Width, bitmap.Height));
DisplayRatio(ratio);
if (zoom < minZoom)
{
zoom = minZoom;
}
else if (zoom > 100)
{
zoom = 100;
}

pictureBox.Image = bitmap;
Zoom();
}

/// <summary>
/// ClientSizeを画面からはみ出ない大きさに調整して設定する.
/// 缩放图片的时候,更改winform的大小.
/// </summary>
/// <param name="size"></param>
private double SetClientSize(System.Drawing.Size size)
private void Zoom()
{
var screenSize = Screen.PrimaryScreen.Bounds.Size;
var ratioX = (double)screenSize.Width / size.Width;
var ratioY = (double)screenSize.Height / size.Height;
var ratio = Math.Max(ratioX, ratioY);
ratio = ReformRatio(ratio);
size.Width = Convert.ToInt32(size.Width * ratio);
size.Height = Convert.ToInt32(size.Height * ratio);
ClientSize = size;
pictureBox.Size = size;
return ratio;
if (bitmap != null)
{
var bitmap2 = new Bitmap(bitmap, bitmap.Width * zoom, bitmap.Height * zoom);
using (Graphics graphics = Graphics.FromImage(bitmap2))
{
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
graphics.DrawImage(bitmap, new Rectangle(0, 0, bitmap2.Width, bitmap2.Height));
}
this.BackgroundImage = bitmap2;
this.ClientSize = bitmap2.Size;
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

this.BackgroundImage = bitmap;
this.BackgroundImageLayout = ImageLayout.Center;

while (BackgroundImage.Size.Width < 120|| BackgroundImage.Size.Height<80)
{
zoom++;
Zoom();
minZoom = zoom; }
this.ClientSize = BackgroundImage.Size;

}
private void MainForm_MouseMove(object sender, MouseEventArgs e)
{
//获取当前鼠标处的像素点的值

if (isDragging)
{
this.Left += e.X - offsetX;
this.Top += e.Y - offsetY;
}
else if (bitmap != null)
{
try
{
var color = bitmap.GetPixel(e.X / zoom, e.Y / zoom);
this.Text = $"看图-X:{e.X / zoom} Y:{e.Y / zoom} R:{color.R} G:{color.G} B:{color.B}";
}
catch (Exception ex)
{

}

private double ReformRatio(double ratio)
}

}
private bool isDragging;
private int offsetX;
private int offsetY;
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
var v1 = ratio;
var lg2 = Math.Log(v1, 2);
var lgz = Math.Floor(lg2);
var pw = lgz == lg2 ? lgz - 1 : lgz;
var r = Math.Pow(2, pw);
return r;
if (e.Button == MouseButtons.Left)
{
isDragging = true;
offsetX = e.X;
offsetY = e.Y;
}
}

private void DisplayRatio(double ratio)


private void MainForm_MouseUp(object sender, MouseEventArgs e)
{
Text = $@"ImageViewer Zoom: {ratio:P1}";
if (e.Button == MouseButtons.Left)
{
isDragging = false;
}
}
}
}
39 changes: 11 additions & 28 deletions src/OpenCvSharp.DebuggerVisualizers/ImageViewer.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.