如何使用c#将jpg文件转换为位图

本文关键字:转换 位图 文件 jpg 何使用 | 更新日期: 2023-09-27 18:10:13

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;
namespace convert
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load_1(object sender, EventArgs e)
        {
           // Image image = Image.FromFile(@"C:'Users'Public'Pictures'Sample Pictures'Koala.jpg");
            // Set the PictureBox image property to this image.
            // ... Then, adjust its height and width properties.
           // pictureBox1.Image = image;
            //pictureBox1.Height = image.Height;
            //pictureBox1.Width = image.Width;
            string strFileName = @"C:'Users'Public'Pictures'Sample Pictures'Koala.jpg";
            Bitmap bitmap = new Bitmap(strFileName);
            //bitmap.Save("testing.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
           pictureBox1.Image = bitmap;
           pictureBox1.Height = bitmap.Height;
           pictureBox1.Width = bitmap.Width;
        }
    }
}

我正在使用上面的代码将jpg文件转换为位图。它的工作原理,但我需要知道如何流jpg图像,并将其转换为位图,然后显示位图图像与存储它。我正在使用c#和vb.net

如何使用c#将jpg文件转换为位图

尝试将此转换为位图:

public Bitmap ConvertToBitmap(string fileName)
{
    Bitmap bitmap;
    using(Stream bmpStream = System.IO.File.Open(fileName, System.IO.FileMode.Open ))
    {
         Image image = Image.FromStream(bmpStream);
         bitmap = new Bitmap(image);
    }
    return bitmap;
}

可能更容易:

var bitmap = new Bitmap(Image.FromFile(path));

可能更容易:

var bitmap = new Bitmap(path);