求助plus 如何实现 上下左右滑动 查看图片

By niheibie at 2023-08-06 • 0人收藏 • 1177人看过

image.png

这是一张1080 * 2280 的图片, 如何能上下左右滑动图片查看呢,各位大佬

13 个回复 | 最后更新于 2023-09-02
2023-08-07   #1
namespace picShow
{
    public partial class Form1 : Form
    {
        //Form2 picForm就是一个空白无边框窗体,大小 200 x 200
        Form2 picForm = new Form2();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            picForm.Show();
            picForm.Visible = false;
            //设置背景图拉伸,实现放大效果
            picForm.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            //定位小窗口位置,可根据需要设置
            picForm.Location = new Point(e.X + this.Location.X - 250, e.Y + this.Location.Y);
            picForm.Visible = true;
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            picForm.Visible = false;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            //功能实现全在这里
            var img = pictureBox1.Image.Clone() as Bitmap;
            var x = e.Location.X * img.Width / pictureBox1.Width<0 ? 0 : e.Location.X * img.Width / pictureBox1.Width;
            var y = e.Location.Y * img.Height / pictureBox1.Height<0 ? 0 : e.Location.Y * img.Height / pictureBox1.Height;

            picForm.BackgroundImage = img.Clone(new Rectangle(x, y, x+100>=img.Width?img.Width-x:100, y+100>img.Height?img.Height-y:100), img.PixelFormat);
        }
    }
}

C# 实现,可局部放大,类似淘宝商品图片功能

屏幕截图 2023-08-07 082323.png

2023-08-07   #2

回复#2 @jerrxjr1220 :

封装成dll类库 , 给aar直接调用,哈

2023-08-07   #3

回复#3 @admin :

这个不用封装吧,都是dotNet和System模块下的东西,aar可以直接调用的。

2023-08-07   #4

回复#4 @jerrxjr1220 :

说的是

2023-08-08   #5

回复#3 @admin :

关键是不会啊,

2023-08-09   #6

c#编写的, 可以拖动, 鼠标转滚轮放大和缩小的图片功能: 双击还原

等我有时间封装个dll给aar用.

screenshots.gif

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pictureBox1.MouseWheel += PictureBox1_MouseWheel;
        }

        bool isMouseDown = false;
        System.Drawing.Point p1 = new System.Drawing.Point();
        System.Drawing.Point p2 = new System.Drawing.Point();

        private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            
            int x = e.Location.X;
            int y = e.Location.Y;
            int ow = pictureBox1.Width;
            int oh = pictureBox1.Height;
            int VX, VY; //因缩放产生的位移矢量
            int zoomStep = (pictureBox1.Width> pictureBox1.Height)? pictureBox1.Width/10: pictureBox1.Height / 10;
            if (e.Delta > 0) //放大
            {
                //防止一直缩成负值
                if (pictureBox1.Width > pictureBox1.Image.Width * 10)
                    return;
                pictureBox1.Width += zoomStep;
                pictureBox1.Height += zoomStep;
            }
            if (e.Delta < 0) //缩小
            {
                //防止一直缩成负值
                if (pictureBox1.Width < pictureBox1.Image.Width / 10)
                    return;

                pictureBox1.Width -= zoomStep;
                pictureBox1.Height -= zoomStep;
            }
            //第④步,求因缩放产生的位移,进行补偿,实现锚点缩放的效果
            VX = (int)((double)x * (ow - pictureBox1.Width) / ow);
            VY = (int)((double)y * (oh - pictureBox1.Height) / oh);
            pictureBox1.Location = new Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);

        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown = true;
            p1 = PointToClient(Control.MousePosition);//记录鼠标坐标
            p2 = pictureBox1.Location;                //记录图片坐标
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            //鼠标坐标的相对改变值
            int a = PointToClient(Control.MousePosition).X - p1.X;
            int b = PointToClient(Control.MousePosition).Y - p1.Y;
            //图片坐标计算&赋值
            if (isMouseDown)
                pictureBox1.Location = new System.Drawing.Point(p2.X + a, p2.Y + b);//图片新的坐标 = 图片起始坐标 + 鼠标相对位移
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            pictureBox1.Size = this.Size;
            pictureBox1.Location = new System.Drawing.Point(0, 0);
        }

        private void Form1_DoubleClick(object sender, EventArgs e)
        {
            pictureBox1.Size = this.Size;
            pictureBox1.Location = new System.Drawing.Point(0, 0);
        }
    }
}


2023-08-09   #7

将上面的C#代码封装成了类库, 可以简单的被aardio调用了.

此类库dll我使用的net4.7.2编译, 如果提示没有这个库, 请自行安装


screenshots.gif



aardio调用代码如下:

import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=903;bottom=623;bgcolor=12639424)
winform.add(
button={cls="button";text="Button";left=142;top=488;right=336;bottom=538;db=1;dl=1;z=2};
custom={cls="custom";text="自定义控件";left=98;top=51;right=727;bottom=413;bgcolor=12632256;db=1;dl=1;dr=1;dt=1;z=1}
)
/*}}*/

import dotNet;
import System.Windows.Forms;
var Forms = System.Windows.Forms;
//.NET 控件必须用 Forms.CreateEmbed 创建一个窗口包住再嵌入 aardio 窗口
var pictureBox1 = Forms.CreateEmbed("PictureBox",winform.custom); 
//声明
var PicBoxExdll = dotNet.load("/PicBoxEx.dll");
var PicBox = PicBoxExdll.new("PicBoxEx.ToAardio");
PicBox.LinkTo(pictureBox1);
PicBox.ChangeImg(io.fullpath("\单波长直对准.png"));
  

winform.button.oncommand = function(id,event){
	PicBox.ChangeImg(io.fullpath("\捕获.PNG"));
}

winform.show();
win.loopMessage();

aardio示例工程下载:

Debug.zip


c#类库代码如下:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace PicBoxEx
{

    public class ToAardio:Form
    {
        public PictureBox pictureBox = null;
        private bool isMouseDown = false;
        private System.Drawing.Point p1 = new System.Drawing.Point();
        private System.Drawing.Point p2 = new System.Drawing.Point();
        public void LinkTo ( PictureBox picturebox )
        {

            pictureBox = picturebox;
            pictureBox.Size = pictureBox.Parent.Size;
            pictureBox.Location = new System.Drawing.Point(0, 0);
            pictureBox.Dock = DockStyle.None; 
            pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox.MouseWheel += PictureBox_MouseWheel;
            pictureBox.MouseDown += PictureBox_MouseDown;
            pictureBox.MouseUp += PictureBox_MouseUp;
            pictureBox.MouseMove += PictureBox_MouseMove;
            pictureBox.MouseDoubleClick += PictureBox_MouseDoubleClick;
            pictureBox.Parent.MouseDoubleClick += Parent_MouseDoubleClick;
            
        }
        public System.Drawing.Image ChangeImg(string path)
        {
            if (path != string.Empty)
            {
                pictureBox.Size = pictureBox.Parent.Size;
                pictureBox.Location = new System.Drawing.Point(0, 0);
                System.Drawing.Image preimg = pictureBox.Image;
                pictureBox.Image = System.Drawing.Image.FromFile(path); 
                return preimg;
            }
            return null;
        }

        private void Parent_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            pictureBox.Size = pictureBox.Parent.Size;
            pictureBox.Location = new System.Drawing.Point(0, 0);
        }

        private void PictureBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            pictureBox.Size = pictureBox.Parent.Size;
            pictureBox.Location = new System.Drawing.Point(0, 0);
        }

        private void PictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (pictureBox == null)
            {
                return;
            }
            //鼠标坐标的相对改变值
            int a = PointToClient(Control.MousePosition).X - p1.X;
            int b = PointToClient(Control.MousePosition).Y - p1.Y;
            //图片坐标计算&赋值
            if (isMouseDown)
                pictureBox.Location = new System.Drawing.Point(p2.X + a, p2.Y + b);//图片新的坐标 = 图片起始坐标 + 鼠标相对位移
        }

        private void PictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            if (pictureBox == null)
            {
                return;
            }
            isMouseDown = false;
        }

        private void PictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (pictureBox == null)
            {
                return;
            }
            isMouseDown = true;
            p1 = PointToClient(Control.MousePosition);//记录鼠标坐标
            p2 = pictureBox.Location;                //记录图片坐标
        }

        private void PictureBox_MouseWheel(object sender, MouseEventArgs e)
        {
            if (pictureBox == null)
            {
                return;
            }
            int x = e.Location.X;
            int y = e.Location.Y;
            int ow = pictureBox.Width;
            int oh = pictureBox.Height;
            int VX, VY; //因缩放产生的位移矢量
            int zoomStep = (pictureBox.Width > pictureBox.Height) ? pictureBox.Width / 10 : pictureBox.Height / 10;
            if (e.Delta > 0) //放大
            {
                //防止一直缩成负值
                if (pictureBox.Width > pictureBox.Image.Width * 10)
                    return;
                pictureBox.Width += zoomStep;
                pictureBox.Height += zoomStep;
            }
            if (e.Delta < 0) //缩小
            {
                //防止一直缩成负值
                if (pictureBox.Width < pictureBox.Image.Width / 10)
                    return;

                pictureBox.Width -= zoomStep;
                pictureBox.Height -= zoomStep;
            }
            //第④步,求因缩放产生的位移,进行补偿,实现锚点缩放的效果
            VX = (int)((double)x * (ow - pictureBox.Width) / ow);
            VY = (int)((double)y * (oh - pictureBox.Height) / oh);
            pictureBox.Location = new Point(pictureBox.Location.X + VX, pictureBox.Location.Y + VY);
        }
    }
}


c#调用此dll类库示例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApp5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        PicBoxEx.ToAardio PicBox;

        private void Form1_Load(object sender, EventArgs e)
        {
            
            PicBox = new PicBoxEx.ToAardio();
            PicBox.LinkTo(pictureBox1);
            PicBox.ChangeImg("C:\\Users\\popdes\\Desktop\\捕获.PNG");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PicBox.ChangeImg("C:\\Users\\popdes\\Desktop\\单波长直对准.png");

        }
    }
}


2023-08-10   #8

再次直接封装了个c#的控件类库, 使用和调用更简单了.

鼠标左键拖拽, 鼠标滚轮放大缩小, 双击窗口回归原位.

import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=903;bottom=623;bgcolor=12639424)
winform.add(
button={cls="button";text="Button";left=128;top=517;right=322;bottom=567;db=1;dl=1;z=2};
custom={cls="custom";text="自定义控件";left=18;top=7;right=895;bottom=474;bgcolor=12632256;db=1;dl=1;dr=1;dt=1;z=1}
)
/*}}*/

import dotNet;
import System.Windows.Forms;
var Forms = System.Windows.Forms;
//声明
var PicExdll = dotNet.load("/PicEx.dll");
//绑定窗体
var PicBox = PicExdll.new("picEx.UserControl1");
var ctrl = Forms.CreateEmbed(PicBox,winform.custom);
//控件不填充满父窗体
ctrl.Dock = 0/*System.Windows.Forms.DockStyle.None*/;

PicBox.imgPath = io.fullpath("\单波长直对准.png");
winform.button.oncommand = function(id,event){
	PicBox.imgPath = io.fullpath("\捕获.PNG");
}

winform.show();
win.loopMessage();


aardio示例工程下载:

picEx.zip


screenshots.gif



下面是这个picEx控件类库代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace picEx
{
    public partial class UserControl1: PictureBox 
    {
        public UserControl1()
        {
            InitializeComponent();
            UserControl1_Load();
        }

        private bool isInitParent = false;
        private bool isMouseDown = false;
        private System.Drawing.Point p1 = new System.Drawing.Point();
        private System.Drawing.Point p2 = new System.Drawing.Point();

        public string imgPath { 
            set {
                if (value != string.Empty)
                {
                    this.Size = this.Parent.Size;
                    this.Location = new System.Drawing.Point(0, 0);
                    this.Image = System.Drawing.Image.FromFile(value);
                    //
                    if (!isInitParent)
                    {
                        isInitParent = true;
                        this.Parent.MouseDoubleClick += PictureBox_MouseDoubleClick;
                    }
                }
            }
        }

        private void UserControl1_Load()
        {
            this.Location = new System.Drawing.Point(0, 0);
            this.Dock = DockStyle.None;
            this.SizeMode = PictureBoxSizeMode.Zoom;
            this.MouseWheel += PictureBox_MouseWheel;
            this.MouseDown += PictureBox_MouseDown;
            this.MouseUp += PictureBox_MouseUp;
            this.MouseMove += PictureBox_MouseMove;
            this.MouseDoubleClick += PictureBox_MouseDoubleClick;
        }

        private void PictureBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.Image == null)
            {
                return;
            }
            this.Size = this.Parent.Size;
            this.Location = new System.Drawing.Point(0, 0);
        }

        private void PictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.Image == null)
            {
                return;
            }
            //鼠标坐标的相对改变值
            int a = Control.MousePosition.X - p1.X;
            int b = Control.MousePosition.Y - p1.Y;
            //图片坐标计算&赋值
            if (isMouseDown)
                this.Location = new System.Drawing.Point(p2.X + a, p2.Y + b);//图片新的坐标 = 图片起始坐标 + 鼠标相对位移
        }

        private void PictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.Image == null)
            {
                return;
            }
            isMouseDown = false;
        }

        private void PictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.Image == null)
            {
                return;
            }
            isMouseDown = true;
            p1 = Control.MousePosition;//记录鼠标坐标
            p2 = this.Location;                //记录图片坐标
        }

        private void PictureBox_MouseWheel(object sender, MouseEventArgs e)
        {
            if (this.Image == null)
            {
                return;
            }
            int x = e.Location.X;
            int y = e.Location.Y;
            int ow = this.Width;
            int oh = this.Height;
            int VX, VY; //因缩放产生的位移矢量
            int zoomStep = (this.Width > this.Height) ? this.Width / 10 : this.Height / 10;
            if (e.Delta > 0) //放大
            {
                //防止一直无限放大导致异常错误
                if (Math.Max( this.Width,this.Height) > Math.Max(this.Image.Width * 10, this.Image.Height * 10))
                    return;
                this.Width += zoomStep;
                this.Height += zoomStep;
            }
            if (e.Delta < 0) //缩小
            {
                //防止一直缩
                if ( Math.Min( this.Width,this.Height) < Math.Min( this.Image.Width/10, this.Image.Height / 10) )
                    return;

                this.Width -= zoomStep;
                this.Height -= zoomStep;
            }
            //第④步,求因缩放产生的位移,进行补偿,实现锚点缩放的效果
            VX = (int)((double)x * (ow - this.Width) / ow);
            VY = (int)((double)y * (oh - this.Height) / oh);
            this.Location = new Point(this.Location.X + VX, this.Location.Y + VY);
        }
    }
}


c#自己调用这个控件dll代码:

拖动这个dll到vs的控件列表里, 然后拖动usercontrol控件到界面上就可以了.

private void Form1_Load(object sender, EventArgs e)
{
     userControl11.imgPath = "C:\\Users\\popdes\\Desktop\\捕获.PNG";
}


2023-08-10   #9

回复#9 @admin :

2023-08-10   #10

感谢分享,学习一下!

2023-08-13   #11

牛逼!!!

2023-08-24   #12

可以参照这个 https://www.aardio.cc/thread-100-1-1.html

使用aardio原生的plus控件+gdip即可实现

2023-09-02   #13

handycontrol中有个Imageviewer控件已经完成了这个功能,可以直接调用

handycontrol应该是有个winform的版本,可以适配framework 4使用,可以直接嵌入到aardio中。

<Window x:Class="WPFImageViewer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFImageViewer"
        mc:Ignorable="d"
        xmlns:hc="https://handyorg.github.io/handycontrol"
        xmlns:vm="clr-namespace:WPFImageViewer.ViewModels"
        Title="MainWindow" Height="450" Width="800"
        FontFamily="JetBrains Mono" FontSize="14"
        Background="White" WindowStyle="None"
        MouseDown="Window_MouseDown">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="35"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Border Grid.ColumnSpan="2" HorizontalAlignment="Left" Margin="10">
            <TextBlock Text="ImageViewer" Style="{StaticResource TextBlockDefaultPrimary}" FontSize="16" FontWeight="Heavy"/> 
        </Border>
        <hc:ButtonGroup Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" Width="100">
            <Button Content="-" Style="{StaticResource ButtonGroupItemHorizontalFirst}" Click="Window_Minimize"/>
            <Button Content="^" Style="{StaticResource ButtonGroupItemDefault}" Click="Window_Maximize"/>
            <Button Content="X" Style="{StaticResource ButtonGroupItemHorizontalLast}" 
                    Background="Red" Foreground="White" Click="Window_Close"/>
        </hc:ButtonGroup>
        
        <DockPanel x:Name="contentView" Grid.Row="1" Grid.Column="0"  Grid.ColumnSpan="2">
            <hc:ImageViewer DockPanel.Dock="Left, Top" ImageSource="https://img-s-msn-com.akamaized.net/tenant/amp/entityid/AA1g5Pl6.img?w=768&amp;h=957&amp;m=6&amp;x=552&amp;y=653&amp;s=249&amp;d=249"></hc:ImageViewer>
            
        </DockPanel>
    </Grid>
</Window>

C#

using System.Windows;
using System.Windows.Input;

namespace WPFImageViewer;

/// <summary>
///     Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public void Window_Minimize(object sender, RoutedEventArgs e)
    {
        WindowState = WindowState.Minimized;
    }

    public void Window_Maximize(object sender, RoutedEventArgs e)
    {
        if (WindowState != WindowState.Maximized)
            WindowState = WindowState.Maximized;
        else
            WindowState = WindowState.Normal;
    }

    public void Window_Close(object sender, RoutedEventArgs e)
    {
        Application.Current.Shutdown();
    }

    private void Window_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ButtonState == MouseButtonState.Pressed) DragMove();
    }
}

screenshots.gif

登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...