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 原预览框;
       }
    }
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

腾讯AI架构师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值