41

I am making a median filter, the problem is manipulating pixes are only possible in Bitmap. Later I want to show the result in a PictureBox which uses Image. I can't figure out a way to to solve this...Only thing I can think of is using a Stream but no idea how. Help will be appriciated~

private void toolStripPerformMedian_Click(object sender, EventArgs e)
{
    var filtered = Filters.MedianFilter(new Bitmap(_activeImageFile), 3);
    var n = Image.FromStream() //How to do this?
}
Dumbo
  • 13,555
  • 54
  • 184
  • 288

3 Answers3

93

A Bitmap is an Image. It inherits from the Image class.

From MSDN:

[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class Bitmap : Image
vcsjones
  • 138,677
  • 31
  • 291
  • 286
3

This is what has worked for me:

var codeBitmap = new Bitmap(your_info);
Image image = (Image)codeBitmap;
Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
Illia.K
  • 49
  • 1
  • 1
    with out context of which namespace this Image class is being pulled in from, it's a bit confusing. As others have said Bitmap inherits from Image, so any property or argument that excepts image, can take a bitmap, there is no need for this cast. If it is the WIndows.Control.Image and then this won't work and neither will the code above – Dave May 31 '20 at 10:36
  • 1
    @Dave I saw a lot of answers (about converting bitmap to image) and tried many code snippets but none of them, except the one in my answer, worked. – Illia.K May 31 '20 at 11:50
  • Image image = new Bitmap(your_info); //works all by itself. – Chizl Jan 19 '23 at 04:19
1

Check up the namespaces. System.Drawing.Image compatible with bitmap, System.Window.Control.Image - not!

STG
  • 93
  • 5