Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a jpg file into a bitmap, using C#?

Tags:

c#

bitmap

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

    }
}

I am using the above code for converting jpg file into bitmap. It works but I need to know how to stream the jpg image and convert it into bitmap then display the bitmap image with out storing it. I am using c# and vb.net

like image 674
KVK Avatar asked Jun 24 '14 09:06

KVK


Video Answer


2 Answers

Possibly easier:

var bitmap = new Bitmap(Image.FromFile(path));
like image 91
Johan Maes Avatar answered Sep 28 '22 12:09

Johan Maes


Try this to convert to Bitmap :

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;
}
like image 44
Mukesh Modhvadiya Avatar answered Sep 28 '22 12:09

Mukesh Modhvadiya