|
Hello. I am using following code to convert bitmap to BitmapImage, in order to show on System.Windows.Control.Image control.
public BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap)
{
BitmapImage bitmapImage = null;
using (var memory = new System.IO.MemoryStream())
{
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.EndInit();
return bitmapImage;
}
}
public void ShowPicture(Bitmap bmp)
{
pictureBox.Source = ConvertBitmapToBitmapImage(bmp);
return;
}
It does not throw any exception but it does not show picture as well. When I try to view the picture, it opens up WPF visualizer and shows me some values (properties of BitmapImage), while the bitmap is fine.
What is wrong with my code. Thanks for any input.
This world is going to explode due to international politics, SOON.
|
|
|
|
|
I don't know the answer to this, but Googling suggests others might:
c# - loading the source of a BitmapImage in WPF? - Stack Overflow[^]
Essentially, there's a suggestion that StreamSource is only accessed when needed by which time your memory stream will have been disposed. The suggestion is to set CacheOption or something.
I'd so some Googling!
Regards,
Rob Philpott.
|
|
|
|
|
I did google. I know it is some small mistake that I am not seeing. But I did google alot. Thanks again.
This world is going to explode due to international politics, SOON.
|
|
|
|
|
Here's the converter I wrote years ago. It converts a Bitmap to ImageSource (specifically a BitmapFrame ):
using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace ResourcesLibrary
{
[ValueConversion(typeof(Bitmap), typeof(ImageSource))]
public class BitmapToImageSourceConverter : IValueConverter
{
[DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Bitmap bmp = value as Bitmap;
if (bmp == null)
return null;
using (Stream str = new MemoryStream())
{
bmp.Save(str, System.Drawing.Imaging.ImageFormat.Bmp);
str.Seek(0, SeekOrigin.Begin);
BitmapDecoder bdc = new BmpBitmapDecoder(str, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
return bdc.Frames[0];
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed."
- G.K. Chesterton
|
|
|
|
|
Compared to my working code:
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
and
window.Content = imageControl;
window.UpdateLayout();
window.ShowDialog();
Don't know enough about your app ... you may be able to apply .UpdateLayout() somewhere else in the visual tree.
|
|
|
|
|
this should work:
private BitmapImage SetModifiedImage(System.Drawing.Bitmap bmp){
bmp = CreateNonIndexedImage(bmp);
BitmapImage bmpi = new BitmapImage();
bmpi.BeginInit();
bmpi.StreamSource = new System.IO.MemoryStream((byte[])(new System.Drawing.ImageConverter()).ConvertTo(bmp, typeof(byte[])));
bmpi.EndInit();
return bmpi;
}
private System.Drawing.Bitmap CreateNonIndexedImage(System.Drawing.Bitmap bmp){
System.Drawing.Bitmap newbmp = new System.Drawing.Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newbmp);
g.DrawImage(bmp, 0, 0);
return newbmp;
}
You don't need the "CreateNonIndexedImage" normally. I had different sets of input and the difference in the PixelFormat gave me problems when working on the bytes of the bitmap.
Hope this helps.
|
|
|
|
|
If we have three pixels, every pixel has 3 bytes (R,G,B). The value of R is (237,19,119) and the value of G is (24,178,203) and the value of B is (230,18,124). If we print these three pixels using C##, what will be the variations in gray-scale?
|
|
|
|
|
Pardon?
I'm sorry, but that makes no sense at all.
If you want to see what colours are produced, then write a simple program which sets small blocks to teh appropriate colours: Graphics.DrawRectangle[^] should do it.
But just asking that with no explanation on what you are trying to achieve? Not likely to get you the solution you are looking for.
And be aware: Not all monitors render colours the same, not all printers do either (which I why I have to spend ages trying to colour match one of my monitors to my printer...) - so the variation in "grey scale" for three different colours, none of which are grey is going to be a bit moot...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Sorry but this sniffs of homework. We're not going to do your work for you.
|
|
|
|
|
Hira Khalid wrote: what will be the variations in gray-scale? Which grayscale?
32 bit, or just 16 shades of gray?
Draw your pixels on a bitmap, convert the bitmap, inspect the pixel.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hira Khalid wrote: The value of R is (237,19,119) Really, in a single byte?
|
|
|
|
|
There is only one part of this question that makes sense, which is that every pixel has R, G and B, all bytes, which is very likely since that's the most common type of pixel these days. The rest may make sense in some parallel universe but not in this one.
|
|
|
|
|
I have a DLL which exports two methods, getSc and releasesSc. The first returns a handle which is a pointer to a class instance. I wish to call the methods on that class instance. Unfortunately that class is not exported as dumpbin proves:
1 0 00001040 ??4CSmartCard@XXXXXDLLNamespace@@QAEAAV01@ABV01@@Z
2 1 00003E40 _getSc@0
3 2 00003E70 _releaseSc@0
4 3 00003E40 getSc
5 4 00003E70 releaseSc
Is there a neat way to call the method on that class instance from C#/.Net?
The only way I can think of is to create an unmanaged DLL which wraps the other DLL. Thus the second DLL would expose functions to call into the class instance.
|
|
|
|
|
If the class methods are not exported then how will you access them from unmanaged code?
|
|
|
|
|
Well, that is the key question isn't it.
I was wondering if there is some equivalent to the manner in which you invoke methods on a COM interface i.e. you effectively tell it the prototype in the code.
|
|
|
|
|
You still need to know the prototype in order to call the methods. So how do you access these methods once you have a handle to the class object?
|
|
|
|
|
At present you add a DllImport with the prototype. So you might hope for something similar for an unmanaged C++ class instance. But apparently not. Ah well.
Incidentally I know that if the DLL exports the class, you can invoke the methods by passing the instance handle as the first argument to the method. But that is not the case I have.
Anyway, the wrapper is written.
|
|
|
|
|
Quote: The only way I can think of is to create an unmanaged DLL which wraps the other DLL
that would be my first guess, nasty as it is - I needed to use it to do the same sort of thing and 'aggregate' lower level (un-exposed) dll calls into something useable
... Im still looking for a better way myself
|
|
|
|
|
Thanks Garth. At least you confirm I am probably on the right track!
|
|
|
|
|
just thinking, have you tried
Quote: [DllImport("some name.dll", EntryPoint = "_getSc@0",
ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
?
|
|
|
|
|
|
Thanks, I hadn't seen that, but it assumes that the C++ class is exported by the DLL which was not the case with my DLL.
(The DLL is going to be modified so I can simply import the class methods.)
|
|
|
|
|
Hello,
I am developing a simple Windows search application. I will pass the all the search filters (Name, size etc) to Windows explorer using my application. But I am not getting commands or codes to select the 'Sort by' type programmatically in the window that will open.
Is it possible to implement that ?
|
|
|
|
|
how are you interacting with Windows explorer ?
I would have thought a better way was to use
string startFolder = @"c:\start dir\";
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);
IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
and then use LINQ to filter fileList
|
|
|
|
|
I am interacting with Windows explorer using "Explorer search-ms:" command.
|
|
|
|