zoukankan      html  css  js  c++  java
  • 裁剪PNG图片透明部分

    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;
    using System.IO;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;

    namespace DXApplication1
    {
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    ClipPng(Application.StartupPath + "\1.png");
    }
    /// <summary>
    /// 裁剪PNG图片透明部分
    /// </summary>
    /// <param name="filepath"></param>
    public static void ClipPng(string filepath)
    {
    FileInfo fileInfo = new FileInfo(filepath);
    Bitmap bitmap = new Bitmap(filepath);
    Rectangle rectangle = GetRectangle(bitmap);
    Bitmap bakBitmap = bitmap.Clone(rectangle, bitmap.PixelFormat);
    string bakFilePath = $@"{fileInfo.Directory}{fileInfo.Name}-bak{fileInfo.Extension}";
    bakBitmap.Save(bakFilePath, ImageFormat.Png);
    bitmap.Dispose();
    fileInfo.Delete();
    fileInfo = new FileInfo(bakFilePath);
    fileInfo.CopyTo(filepath);
    System.IO.File.Delete(bakFilePath);
    }/// <summary>
    /// 根据图片得到一个图片非透明部分的矩形范围
    /// </summary>
    /// <param name="bckImage"></param>
    /// <returns></returns>
    public static unsafe Rectangle GetRectangle(Bitmap bckImage)
    {
    int w = bckImage.Width;//原宽
    int h = bckImage.Height;//原高
    //x:原图左侧到非透明范围左侧的长度
    //y:原图头部到非透明范围头部的高度
    //width:裁剪后图片宽度
    //height:裁剪后图片高度
    int x = 0, y = 0, width = 0, height = 0;

    BitmapData bckdata = null;
    try
    {
    bckdata = bckImage.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    uint* bckInt = (uint*)bckdata.Scan0;
    for (int j = 0; j < h; j++)
    {
    for (int i = 0; i < w; i++)
    {
    if ((*bckInt & 0xff000000) != 0)
    {
    if (x == 0 || i < x)
    {
    x = i;
    }
    if (y == 0 || j < y)
    {
    y = j;
    }
    if (width == 0 || i > width)
    {
    width = i;
    }
    if (height == 0 || j > height)
    {
    height = j;
    }

    }
    bckInt++;
    }
    }
    bckImage.UnlockBits(bckdata); bckdata = null;
    }
    catch
    {
    if (bckdata != null)
    {
    bckImage.UnlockBits(bckdata);
    bckdata = null;
    }
    }
    int border = 20;//边框大小
    if (x > border)
    {
    x -= border;
    }
    if (y > border)
    {
    y -= border;
    }
    if ((w - width) > border)
    {
    width += border;
    }
    if ((h - height) > border)
    {
    height += border;
    }
    return new Rectangle(x, y, width - x, height - y);
    }
    }
    }

  • 相关阅读:
    ASCII对应码表-键值(完整版)
    node.js中使用路由方法
    关于vscode自动跳转回车的解决方法(关闭vscode自动保存功能;可能和其他插件有冲突)
    js中 !==和 !=的区别是什么
    spring 请求参数和路径变量
    PowerShell因为在此系统中禁止执行脚本解决方法
    SQL server 2008数据库的备份与还原(亲测,效果良好)注意采用单用户模式呀
    webpack-dev-server提示css模块解析失败,但已经装了css-loader
    webpack集成vue单文件模式的很多坑(研究了1个星期)
    npm全局模块卸载及默认安装目录修改方法
  • 原文地址:https://www.cnblogs.com/liushunli/p/14839283.html
Copyright © 2011-2022 走看看