(窗体剪贴板)System.Drawing.Bitmap
(剪贴板)System.Windows.Media.Imaging.BitmapSource
1.从bitmap转换成ImageSource
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);
/// <summary>
/// 从bitmap转换成ImageSource
/// </summary>
/// <param name="icon"></param>
/// <returns></returns>
public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap)
{
//Bitmap bitmap = icon.ToBitmap();
IntPtr hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap,IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());
if (!DeleteObject(hBitmap))
{
throw new System.ComponentModel.Win32Exception();
}
return wpfBitmap;
}
2.从Bitmap转换成BitmapSource
/// <summary>
/// 从Bitmap转换成BitmapSource
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public static BitmapSource ChangeBitmapToBitmapSource(this Bitmap bmp)
{
BitmapSource returnSource;
try
{
returnSource = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());
}
catch
{
returnSource = null;
}
return returnSource;
}
3.从Icon到ImageSource的转换
/// <summary>
/// 从Icon到ImageSource的转换
/// </summary>
public ImageSource ChangeIconToImageSource(Icon icon)
{
ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(icon.Handle,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());
return imageSource;
}
4.从Icon到ImageSource的转换
internal static class IconUtilities
{
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);
public static ImageSource ToImageSource(this Icon icon)
{
Bitmap bitmap = icon.ToBitmap();
IntPtr hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());
if (!DeleteObject(hBitmap))
{
throw new Win32Exception();
}
return wpfBitmap;
}
// 这个是没有附加转换的,:)
public static ImageSource ToImageSource(this Icon icon)
{
ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon( icon.Handle,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
return imageSource;
}
}
5.从ImageSource转换成Bitmap,是从ImageSource中取出UriSource.LocalPath,然后使用 new Bitmap(FileName)的方法获取。其他的方法我还没有找到,
public static void ToImageSource(this Icon icon)
{
System.Windows.Controls.Image ImgUserHeadFaceCutEdit;
string str1 = ((BitmapImage)(ImgUserHeadFaceCutEdit.Source)).UriSource.AbsolutePath;// 此路径new Bitmap(str1)无法识别
string str2 = ((BitmapImage)(ImgUserHeadFaceCutEdit.Source)).UriSource.LocalPath;
//Bitmap sourceImage = new Bitmap(sourceImageUri.ToString());
string str3 = strImgSourceFileName;
Console.WriteLine("AbsolutePath =" + str1);
Console.WriteLine("LocalPath =" + str2);
Console.WriteLine("srceFileName =" + str3);
}
//这是运行结果:
//AbsolutePath =C:/Documents%20and%20Settings/zp/%E6%A1%8C%E9%9D%A2/%E6%A1%8C%E9%9D%A2%E7%A7%80/10111411409225.jpg
//LocalPath =C:\Documents and Settings\zp\桌面\桌面秀\10111411409225.jpg
//srceFileName =C:\Documents and Settings\zp\桌面\桌面秀\10111411409225.jpg
BitmapSource image = null;
if (Clipboard.ContainsImage())
{
image = (BitmapSource)System.Windows.Clipboard.GetImage();
}
MemoryStream ms = new MemoryStream();
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(image));
enc.Save(ms);
Bitmap img = new Bitmap(ms);
ms.Flush();
ms = new MemoryStream();
img.Save(ms, ImageFormat.Jpeg);
FileStream fs = new FileStream(lpszPath + lpszName, FileMode.Create);
fs.Write(ms.ToArray(), 0, ms.ToArray().Length);
fs.Flush();
fs.Dispose();
ms = null;