【问题标题】:How can I convert a jpg file into a bitmap, using C#?如何使用 C# 将 jpg 文件转换为位图?
【发布时间】:2014-06-24 09:39:47
【问题描述】:
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# bitmap


【解决方案1】:

试试这个转换成位图:

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 应用程序中使用网格视图执行此过程。在网格中没有位图只有位图图像可用。我该怎么做
  • 很好,我对 WPF 没有太多经验,但是如果你需要的话,看看这个链接 wpf.codeplex.com/discussions/61943
【解决方案2】:

可能更容易:

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

【讨论】:

    猜你喜欢
    • 2010-12-16
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多