winform如何做出类似淘宝那种缩略图显示技术

wzmlove007 2016-08-01 09:28:24
大家在浏览淘宝的时候当鼠标放在图片上面时,在右边会弹出图片的大图。
这个在网页程序中用一段js可以实现,可是如何在winform中实现这种功能,
或者是不是有什么控件可以实现这功能。
...全文
1021 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zbdzjx 2016-08-10
  • 打赏
  • 举报
回复
一个简单的例子:
1、如下图,放两个pictureBox,一个button,四个label。右边的pictureBox设定为隐藏。


2、代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test010
{
public partial class Form1 : Form
{
string PicPath = @"C:\VS2012Test\Test010\Test010\images\img18.jpg";

int RealWidth = 0;
int RealHeight = 0;

int ShowWidth = 0;
int ShowHeight = 0;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Load(PicPath);

Image pic = Image.FromFile(PicPath);
RealWidth = pic.Width;
RealHeight = pic.Height;

ShowWidth = pictureBox1.Width;
ShowHeight = pictureBox1.Height;
}

public Image AcquireRectangleImage(Image source, Rectangle rect) //截取大图指定范围的小图
{
if (source == null || rect.IsEmpty) return null;
Bitmap bmSmall = new Bitmap(rect.Width, rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

using (Graphics grSmall = Graphics.FromImage(bmSmall))
{
grSmall.DrawImage(source, new System.Drawing.Rectangle(0, 0, bmSmall.Width, bmSmall.Height), rect, GraphicsUnit.Pixel);
grSmall.Dispose();
}
return bmSmall;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (RealWidth > 0)
{
using (var pict = new PictureBox())
{
int MouseX = e.X;
int MouseY = e.Y;
pict.Load(PicPath);
var im = AcquireRectangleImage(pict.Image, new Rectangle(MouseX * RealWidth / ShowWidth - pictureBox2.Width / 2, MouseY * RealHeight / ShowHeight - pictureBox2.Height / 2, pictureBox2.Width, pictureBox2.Height));
pictureBox2.Image = im;

label1.Text = MouseX.ToString();
label2.Text = MouseY.ToString();

label3.Text = (MouseX * RealWidth / ShowWidth - pictureBox2.Width / 2).ToString();
label4.Text = (MouseY * RealHeight / ShowHeight - pictureBox2.Height / 2).ToString();
}
}
}

private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pictureBox2.Show();
}

private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox2.Hide();
}
}
}
exception92 2016-08-03
  • 打赏
  • 举报
回复
WPF 设置Image的toopTip 为放大后的图片。 或者 用devexpress 第三方。

8,834

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 组件/控件开发
社区管理员
  • 组件/控件开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧