首页 > 编程语言 >C# png格式图像转jpg时透明背景变为黑色问题

C# png格式图像转jpg时透明背景变为黑色问题

时间:2022-12-17 10:56:15浏览次数:37  
标签:format C# Image System jpg ImageFormat ms image png

功能需求:

1、把一张图片(png bmp jpeg bmp gif)转换为byte数组存放到数据库。

2、把从数据库读取的byte数组转换为Image对象,赋值给相应的控件显示。

3、从图片byte数组得到对应图片的格式,生成一张图片保存到磁盘上。

注意事项:JPG后缀图片转成type[],再重新转回来图片背景色会变成黑色。

应该是要PNG后缀的图片格式保存,不然透明背景色会变成黑色。

这里的Image是System.Drawing.Image。

      //Get an image from file
        Image image = Image.FromFile("D:\\test.jpg");
        Bitmap bitmap = new Bitmap("D:\\test.jpg");

以下三个函数分别实现了上述三个需求:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;

namespace NetUtilityLib
{
    public static class ImageHelper
    {
        /// <summary>
        /// Convert Image to Byte[]
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static byte[] ImageToBytes(Image image)
        {
            ImageFormat format = image.RawFormat;
            using (MemoryStream ms = new MemoryStream())
            {
                if (format.Equals(ImageFormat.Jpeg))
                {
                    image.Save(ms, ImageFormat.Jpeg);
                }
                else if (format.Equals(ImageFormat.Png))
                {
                    image.Save(ms, ImageFormat.Png);
                }
                else if (format.Equals(ImageFormat.Bmp))
                {
                    image.Save(ms, ImageFormat.Bmp);
                }
                else if (format.Equals(ImageFormat.Gif))
                {
                    image.Save(ms, ImageFormat.Gif);
                }
                else if (format.Equals(ImageFormat.Icon))
                {
                    image.Save(ms, ImageFormat.Icon);
                }
                byte[] buffer = new byte[ms.Length];
                //Image.Save()会改变MemoryStream的Position,需要重新Seek到Begin
                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(buffer, 0, buffer.Length);
                return buffer;
            }
        }

        /// <summary>
        /// Convert Byte[] to Image
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static Image BytesToImage(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer);
            Image image = System.Drawing.Image.FromStream(ms);
            return image;
        }

        /// <summary>
        /// Convert Byte[] to a picture and Store it in file
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static string CreateImageFromBytes(string fileName, byte[] buffer)
        {
            string file = fileName;
            Image image = BytesToImage(buffer);
            ImageFormat format = image.RawFormat;
            if (format.Equals(ImageFormat.Jpeg))
            {
                file += ".jpeg";
            }
            else if (format.Equals(ImageFormat.Png))
            {
                file += ".png";
            }
            else if (format.Equals(ImageFormat.Bmp))
            {
                file += ".bmp";
            }
            else if (format.Equals(ImageFormat.Gif))
            {
                file += ".gif";
            }
            else if (format.Equals(ImageFormat.Icon))
            {
                file += ".icon";
            }
            System.IO.FileInfo info = new System.IO.FileInfo(file);
            System.IO.Directory.CreateDirectory(info.Directory.FullName);
            File.WriteAllBytes(file, buffer);
            return file;
        }
    }
}

 

标签:format,C#,Image,System,jpg,ImageFormat,ms,image,png
From: https://www.cnblogs.com/weifeng123/p/16988688.html

相关文章

  • adcs-ec1打法
    参考链接https://forum.butian.net/share/1583https://forum.butian.net/share/1607环境介绍dc2012192.168.114.32win2016192.168.114.36ldap下域内证书配置查......
  • AbstractQueuedSynchronizer
    1.独占模式1.1获取锁//需要子类实现的方法protectedbooleantryAcquire(intarg){thrownewUnsupportedOperationException();}/......
  • ADCS-EC4
    参考链接https://redteam.wiki/postexploitation/active-directory/adcs/esc4https://www.wangan.com/p/7fy747b040b15bd7#ADCSESC2https://burmat.gitbook.io/securit......
  • linux(centos)创建虚拟环境并部署airflow
    一、创建虚拟环境1.安装virtualenv,virtualenvwrapperpipinstallvirtualenvvirtualenvwrapper安装好之后创建存放虚拟环境目录,这里我存在~/pythonvenv里,查看当前......
  • KeyShot Pro for mac(3D渲染和动画制作软件) v11.3.2.3激活版
    KeyShot11是一款优秀的专业化实时3D渲染工具,使用它可以简化3d渲染和动画制作流程,并且提供最准确的材质及光线,渲染效果更加真实,KeyShot为您提供了使用CPU或NVIDIAGPU进......
  • VSCode编辑器极简使用入门
    VSCode(VisualStudioCode)是一款开源、跨平台、轻量级的代码编辑器,具有非常丰富的插件生态。他本身就是JavaScript+Electron(/ɪˈlektrɒn/电子)代码开发的。官方下载......
  • C/C++数据结构课程设计[长春理工大学计算机科学技术学院2022秋季学期]
    C/C++数据结构课程设计[长春理工大学计算机科学技术学院2022秋季学期]长春理工大学计算机科学技术学院2022秋季学期数据结构课程设计一、目的:巩固数据结构与算法课内......
  • 链接--C++相关问题
    C++的一些语言特性使之必须和编译器链接器共同支持才能工作。重复代码消除全局构造和析构重复代码消除:C++编译器在很多时候会产生重复的代码,比如模板(Templates)、......
  • 第十四章《多线程》第9节:ThreadLocal类
    如果多个线程共用一个对象,那么这个对象的属性值对于这些线程都是相同的。例如有一个a对象,它有一个x属性,如果x属性的值是1,那么对于任何一个线程而言,a对象的x属性都是1。但有......
  • 第十五章《网络编程》第3节:基于TCP协议的网络编程
    如果希望开发一个类似于QQ那样的即时通信软件,就必须使用基于TCP协议的编程技术。基于TCP协议的编程可以实现在通信两端建立虚拟网络链路,这样的话通信两端的程序就能通过虚拟......