0
点赞
收藏
分享

微信扫一扫

c#图像处理-图像预览全解

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 util
{
public class 图片预览类
{
public static PictureBox 显示缩放(Image 原图片,PictureBox 原图片框,Point 缩放中心,float 缩放倍数)
{
Size yuan = new Size(缩放中心.X - 原图片框.Location.X, 缩放中心.Y - 原图片框.Location.Y);
int 新宽 = (int)(原图片框.Width * 缩放倍数);
int 新高 = (int)(原图片框.Height * 缩放倍数);
Image 显示 = 原图片.GetThumbnailImage(新宽, 新高, new Image.GetThumbnailImageAbort(IsTrue), IntPtr.Zero);
原图片框.Image = 显示;
原图片框.Size = new Size(显示.Width, 显示.Height);
原图片框.Location = new Point(缩放中心.X - (int)(yuan.Width * 缩放倍数), 缩放中心.Y - (int)(yuan.Height * 缩放倍数));
GC.Collect();
return 原图片框;
}
private static bool IsTrue() // 在 Image 类别对图片进行缩放的时候,需要一个返回 bool 类别的委托
{ return true; }

public static PictureBox 图片打开(PictureBox 原预览框, Size 图片最大显示, Point 中心位置)
{
Image 显示;
Image 原图片 = 原预览框.Image;
if (原图片.Width < 图片最大显示.Width && 原图片.Height < 图片最大显示.Height)
{
显示 = new Bitmap(原图片.Width, 原图片.Height);
显示 = 图像处理类.图片缩放(原图片, 原图片.Width, 原图片.Height);
原预览框.Image = 显示;
原预览框.Size = new Size(原图片.Width, 原图片.Height);
原预览框.Location = new Point(中心位置.X - 原图片.Width / 2, 中心位置.Y - 原图片.Height / 2);
}
if (原图片.Width > 图片最大显示.Width || 原图片.Height > 图片最大显示.Height)
{
float 宽高比 = (float)原图片.Width / 原图片.Height;//1150/500=2.3
if (宽高比 > 2.3)
{
显示 = new Bitmap(图片最大显示.Width, (int)(图片最大显示.Width / 宽高比));
显示 = 图像处理类.图片缩放(原图片, 图片最大显示.Width, (int)(图片最大显示.Width / 宽高比));
原预览框.Size = new Size(图片最大显示.Width, (int)(图片最大显示.Width / 宽高比));
原预览框.Location = new Point(中心位置.X - 显示.Width / 2, 中心位置.Y - 显示.Height / 2);
}
else
{
显示 = new Bitmap((int)(图片最大显示.Height * 宽高比), 图片最大显示.Height);
显示 = 图像处理类.图片缩放(原图片, (int)(图片最大显示.Height * 宽高比), 图片最大显示.Height);
原预览框.Size = new Size((int)(图片最大显示.Height * 宽高比), 图片最大显示.Height);
原预览框.Location = new Point(中心位置.X - 显示.Width / 2, 中心位置.Y - 显示.Height / 2);
}
原预览框.Image = 显示;
}
GC.Collect();
return 原预览框;
}
}
}


举报

相关推荐

0 条评论