I have an image copied to the clicpboard from the word using "Comment.Scope.CopyAsPicture()"
How can i find the type of this image in the clipboard, so that i can write the image with proper extension in local file system?
In DataFromats we have only Bitmap member...
normally you choose the format to store
IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Bitmap))
{
Bitmap bitmap = (data.GetData(DataFormats.Bitmap,true) as Bitmap);
bitmap.Save("image.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
bitmap.Save("image.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
but You can create Your own format and use it
DataFormats.Format jepgFormat = DataFormats.GetFormat("jpgFormat");
Image image = new Image();
DataObject myDataObject = new DataObject(jpegFormat.Name, image );
// Copies myObject into the clipboard.
Clipboard.SetDataObject(myDataObject);
Using DataFormats.Bitmap you can get this as a bitmap and use the framework functions to save it.
if (iData.GetDataPresent(DataFormats.Bitmap))
Bitmap bImg = (Bitmap)iData.GetData();
Related
I am using C# to implement JKFinger SDK into our office project and I am new to C#. There are two methods in SDK, PrintImageAt() which is drawing image into PictureBox And GetFingerImage() get the last captured image by device in bmp format.
Graphics g = pictureBox1.CreateGraphics();
int dc = g.GetHdc().ToInt32();
axZKFPEngX1.PrintImageAt(dc, 0, 0, axZKFPEngX1.ImageWidth, axZKFPEngX1.ImageHeight);
object obj = null;
axZKFPEngX1.GetFingerImage(ref obj);
Now I want to store the captured image into database. The PictureBox is displaying the images but not initializing with it. For getting image from PictureBox is returning null. And for GetFingerPrint(ref obj) is returning an object but can't be converted to Bitmap. I want to know how to get that image.
GetFingerPrint(ref obj) Is returning System.Byte[] Object.
axZKFPEngX1.GetFingerImage(ref obj);
byte[] data = (byte[])obj;
MemoryStream ms = new MemoryStream(data);
Image image = Image.FromStream(ms);
I am programming in a WPF application in c#. I need to change the notifier icon sometimes;
I implemented the icon like this:
<tn:NotifyIcon x:Name="MyNotifyIcon"
Text="Title"
Icon="Resources/logo/Error.ico"/>
My solution is changing the Icon, the type of MyNotifyIcon.Icon is ImageSource, and I want to get by an icon file. I could find the way to do that.
Do somebody have some ideas how? Or have the any other solution?
Shortly, I have an address like /Resource/logo.icon, and I wanna get a System.Windows.Media.ImageSource
You can use the BitmapImage class:
// Create the source
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri("./Resource/logo/logo.icon");
img.EndInit();
BitmapImage class inherits from ImageSource class which means you can pass the BitmapImage object to NotifyIcon.Icon as:
NI.Icon = img;
Are you using the NotifyIcon from the Hardcodet.Wpf.TaskbarNotification namespace created by Philipp Sumi? If so you have the option to either specify the icon as an ImageSource or an Icon.
TaskbarIcon notifyIcon = new TaskbarIcon();
// set using Icon
notifyIcon.Icon = some System.Drawing.Icon;
// set using ImageSource
notifyIcon.IconSource = some System.Windows.Media.ImageSource;
Note that internally setting IconSource sets Icon.
To set from a resource.
notifyIcon.Icon = MyNamespace.Properties.Resources.SomeIcon
have you thought in do it with resources?
To get from resources:
MainNameSpace.Properties.Resources.NameOfIconFileInResources;
Put the icons in resources, if you have the image file (not icon) i have a method to change it:
public static Icon toIcon(Bitmap b)
{
Bitmap cb = (Bitmap) b.Clone();
cb.MakeTransparent(Color.White);
System.IntPtr p = cb.GetHicon();
Icon ico = Icon.FromHandle(p);
return ico;
}
And programmatically change it with the known attribute .Icon;
Don't worry about ImageSource type.
Extracting image from icon (.ico) file:
Stream iconStream = new FileStream (MainNameSpace.Properties.Resources.NameOfIconFileINResources, FileMode.Open );
IconBitmapDecoder decoder = new IconBitmapDecoder (
iconStream,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.None );
// loop through images inside the file
foreach ( var item in decoder.Frames )
{
//Do whatever you want to do with the single images inside the file
this.panel.Children.Add ( new Image () { Source = item } );
}
// or just get exactly the 4th image:
var frame = decoder.Frames[3];
// save file as PNG
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(frame);
using ( Stream saveStream = new FileStream ( #"C:\target.png", FileMode.Create ))
{
encoder.Save( saveStream );
}
But you must put the .ico file in resources, or take it with relative path...
Taken from How can you access icons from a Multi-Icon (.ico) file using index in C#
I'm developing a control where user can set an image and i want this this to be as user friendly as possible - so support for copy & paste, drag & drop.
I've got this part working using IDataObjects, testing for fileformats of FileDrop, FileContents (eg from outlook), and bitmap eg:
private void GetImageFromIDataObject(IDataObject myIDO)
{
string[] dataformats = myIDO.GetFormats();
Boolean GotImage = false;
foreach (string df in dataformats)
{
if (df == DataFormats.FileDrop)
{
// code here
}
if (df == DataFormats.Bitmap)
{
// Source of my problem here... this gets & displays image but
// how do I then convert from here ?
ImageSource myIS = Utilities.MyImaging.ImageFromClipboardDib();
ImgPerson.Source = myIS;
}
}
}
The ImageFromClipboard code is Thomas Levesque's as referenced in the answer to this SO question wpf InteropBitmap to bitmap
http://www.thomaslevesque.com/2009/02/05/wpf-paste-an-image-from-the-clipboard/
No matter how I get the image onto ImgPerson, this part is working fine; image displays nicely.
When user presses save I need to convert the image to a bytearray and send to a WCF server which will save to server - as in, reconstruct the bytearray into an image and save it in a folder.
For all formats of drag & drop, copy & paste the image is some form of System.Windows.Media.Imaging.BitmapImage.
Except for those involving the clipboard which using Thomas's code becomes System.Windows.Media.Imaging.BitmapFrameDecode.
If I avoid Thomas's code and use:
BitmapSource myBS = Clipboard.GetImage();
ImgPerson.Source = myBS;
I get a System.Windows.Interop.InteropBitmap.
I can't figure out how to work with these; to get them into a bytearray so I can pass to WCF for reconstruction and saving to folder.
Try this piece of code
public byte[] ImageToBytes(BitmapImage imgSource)
{
MemoryStream objMS = new MemoryStream();
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(imgSource));
encoder.Save(objMS);
return objMS.GetBuffer();
}
You can also use JpegBitmapEncoder, BmpBitmapEncoder based on your requirements.
byte[] arr = ImageToBytes(ImgPerson.Source as BitmapImage);
I can't believe I didn't see this SO question but the this is essentially the same as my question:
WPF: System.Windows.Interop.InteropBitmap to System.Drawing.Bitmap
The answer being:
BitmapSource bmpSource = msg.ThumbnailSource as BitmapSource;
MemoryStream ms = new MemoryStream();
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmpSource));
encoder.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(ms);
So similar in execution to Nitesh's answer but crucially, works with an inter-op bitmap.
public Image Image { get; set; }
Image=(Image)randomImageFromCommentsClass.Image;
I get this:
Cannot convert System.Drawing.Image to System.Web.UI.WebControls
I think I imported the wrong namespace (System.Drawing)..That may be the mistake. What I am trying to do is to convert the Image type from database to an Image object.
public class Comments
{
public Image Image { get; set; }
Image = DBNull.Value.Equals(dr["Avatar"]) ? null: (Image)dr["Image"];
This image property above is from a different class code file in visual studio 2010.
Somehow the cast fails..how do i fix that?
There is definitely no way you will be able to cast unless the object you are casting is an instance of the other object.
You can covert the byte array to an Image with this snipet:
public Image byteArrayToImage(byte[] byteArrayIn){
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage
}
To show a dynamically created image with your WebControl you will need a handler more info here:
Serving Dynamic Content with HTTP Handlers
If you are talking about SQL's Image datatype then these images are stored in byte[] format so u will need to take the data from database into byte[] variable and then using FileStream create a new temporary Image file in your fileSystem and then assign it to any image control or whatever you want to do.
are you trying to assign image stored in database to Image Control in ASP.NET?
I dont think there is any way to convert Byte[] to System.Drawing
so here is the solution i use:
byte[] ImageData;
string filePath = #"~\Image.jpeg"; //path or temporary Image
using (con)
{
con.Open();
SqlCommand getImageCmd = new SqlCommand("/* your SQL query to get Image from database*/ ", con);
ImageData = (byte[])getImageCmd.ExecuteScalar();
con.Close();
}
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
using (fs)
{
foreach (byte b in ImageData)
{
fs.WriteByte(b);
}
fs.Flush();
fs.Close();
}
ImageControl.ImageUrl = "~/Image.jpeg"; // assign that temp Image to Image Control
This article shows how to create an Image control that will accept an image directly from memory with no file saving or handlers required. Hope it's helpful.
http://www.eggheadcafe.com/tutorials/aspnet/e1a14e2c-e746-4bed-a552-24c632bd2709/aspnet-inmemory-image-control-with-builtin-resizing-of-posted-file.aspx
I have an image control with a source image located in my c drive. I get a message that the image is being used by another process whenever I try to delete the original image to change it with another one dynamically. How do I release the image from the image control to be able to delete it.
I tried this variants:
string path = ((BitmapImage)img.Source).UriSource.LocalPath;
img.SetValue(System.Windows.Controls.Image.SourceProperty, null);
File.Delete(path);
And:
string path = ((BitmapImage)img.Source).UriSource.LocalPath;
img.Source = null;
File.Delete(path)
But it's not work...
Try setting the bitmap image through the stream source property. That way the app won't put a lock on the file since you loaded it through a stream.
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.streamsource(VS.85).aspx
//this function allows you to load an image from a file and release it
BitmapImage loadPhoto(string path)
{
BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.CacheOption = BitmapCacheOption.OnLoad;
bmi.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bmi.UriSource = new Uri(path);
bmi.EndInit();
return bmi;
}