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

11
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。


1
位图 bmp = new 位图(new 内存流(blob)); - Rachel Gallen
https://dev59.com/FUbRa4cB1Zd3GeqPwgy1 - Rachel Gallen
3个回答

18

尝试使用以下方法将其转换为位图:

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;
}

这个程序运行良好。但我需要在WPF应用程序中使用GridView执行此过程。在网格中,没有位图,只有位图图像可用。我该怎么做? - KVK
不错,我对 WPF 没有太多经验,但你可以查看一下这个链接 https://wpf.codeplex.com/discussions/61943 ,如果它是你需要的。 - Mukesh Modhvadiya
我怎么在我的程序中使用Bitmap?Visual Studio说找不到Bitmap。我该怎么办? - Max Coplan

7
可能更容易理解:
var bitmap = new Bitmap(Image.FromFile(path));

0
可能甚至更容易:
var bitmap = new Bitmap(path);

请参见这里的参考。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接