鱼羊和瓜瓜
2021-08-03 22:18
采纳率: 100%
浏览 64

c# 图像格式png或bmp转换为ImageBGRA32

各位大咖,想要把一幅图片png格式或bmp格式转换为ImageBGRA32格式。
public ImageBGRA32(uint width, uint height, IntPtr imageData);

  • 收藏

2条回答 默认 最新

  • CSDN专家-showbo 2021-08-04 10:22
    已采纳

    有帮助麻烦点个采纳【本回答右上角】,谢谢~~下面2张图为将原bgr32格式转换为bgra32的格式

    img

    img

    //项目需要引用PresentationCore.dll和WindowsBase,然后导入下面3个ns
    //using System.Windows.Media.Imaging;//需要引用PresentationCore.dll
    //using System.Windows.Media;
    //using System.IO;
    //使用方式
    //ImageToBGRA32(@"C:\Users\Admin\Desktop\图片\OBS_Bgra.jpg", @"C:\Users\Admin\Desktop\图片\OBS_Bgra32.jpg");
            public BitmapImage ImageToBitmapImage(string path)
            {
                using (Image img = new Bitmap(path)) 
                {
                    using (var memory = new System.IO.MemoryStream())
                    {
                        img.Save(memory, ImageFormat.Png);
                        memory.Position = 0;
    
                        var bitmapImage = new BitmapImage();
                        bitmapImage.BeginInit();//需要添加WindowsBase引用
                        bitmapImage.StreamSource = memory;
                        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                        bitmapImage.EndInit();//需要添加WindowsBase引用
    
                        return bitmapImage;
                    }
                }
            }
            /// <summary>
            /// 将目标图片转换为bgra32
            /// </summary>
            /// <param name="path">原图片</param>
            /// <param name="tpath">转换后图片保存物理路径</param>
            public void ImageToBGRA32(string path,string tpath)
            {
                BitmapImage bi = ImageToBitmapImage(path);
                if (bi.Format != PixelFormats.Bgra32)
                {
                    FormatConvertedBitmap fcb = new FormatConvertedBitmap(bi, PixelFormats.Bgra32, null, 0);
    
                    var pngBitmapEncoder = new PngBitmapEncoder();
                    pngBitmapEncoder.Interlace = PngInterlaceOption.Off;
                    pngBitmapEncoder.Frames.Add(BitmapFrame.Create(fcb));
    
                    var memstream = new MemoryStream();
                    pngBitmapEncoder.Save(memstream);
                    memstream.Position = 0;
    
    
                    var fs = new FileStream(tpath, FileMode.OpenOrCreate);
                    var bw = new BinaryWriter(fs);
                    bw.Write(memstream.ToArray());
                    bw.Close();
                    fs.Close();
                }
            }
    
    
    已采纳该答案
    1 打赏 评论
  • 参考:

    SaveFileDialog sfd = new SaveFileDialog();
    sfd.Title = "图象另存为";
    sfd.OverwritePrompt = true;
    sfd.CheckPathExists = true;
    sfd.Filter = cmbSaveFiletype.Text + "|" + cmbSaveFiletype.Text;
    sfd.ShowHelp = true;
    if(sfd.ShowDialog() == DialogResult.OK)
    {
    string strFileName = sfd.FileName;
    switch(cmbSaveFiletype.Text)
    {
    case "*.bmp":
    m_bitmap.Save(strFileName, ImageFormat.Bmp);
    break;
    
    case "*.jpg":
    m_bitmap.Save(strFileName, ImageFormat.Jpeg);
    break;
    
    case "*.gif":
    m_bitmap.Save(strFileName, ImageFormat.Gif);
    break;
    
    case "*.tif":
    m_bitmap.Save(strFileName, ImageFormat.Tiff);
    break;
    }
    MessageBox.Show("图象文件格式转换成功!", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    
    打赏 评论

相关推荐 更多相似问题