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

    }
}

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

Reporter
  • 3,897
  • 5
  • 33
  • 47
KVK
  • 1,257
  • 2
  • 17
  • 48

3 Answers3

18

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;
}
Mukesh Modhvadiya
  • 2,178
  • 2
  • 27
  • 32
  • this works fine.but i need to do this process in wpf application using grid view.in grid no bitmap only bitmapimages only available.how can i do that – KVK Jun 24 '14 at 11:37
  • That's good, I have not much experience with WPF but Have a look at this link https://wpf.codeplex.com/discussions/61943, if it is what you need. – Mukesh Modhvadiya Jun 24 '14 at 12:52
  • How do I use `Bitmap` in my program? Visual Studio says it cannot find Bitmap. What do I do? – Max Coplan Sep 02 '23 at 22:35
7

Possibly easier:

var bitmap = new Bitmap(Image.FromFile(path));
Johan Maes
  • 1,161
  • 13
  • 13
0

Possibly even easier:

var bitmap = new Bitmap(path);

See ref here.

malat
  • 12,152
  • 13
  • 89
  • 158