12,162
社区成员
发帖
与我相关
我的任务
分享
System.Collections.Specialized.StringCollection fileList = Clipboard.GetFileDropList();//获得文件名
if (fileList.Count > 0)
{
cmsMain.Enabled = false;
Thread thread = new Thread(new ParameterizedThreadStart(SaveBrowserPic));
thread.IsBackground = true;
thread.Start(fileList[0]);//复制的图片本地文件路径
}
else if (Clipboard.ContainsImage())
{
Thread thread = new Thread(new ParameterizedThreadStart(SaveBrowserPic2));
thread.IsBackground = true;
thread.Start(Clipboard.GetImage());//保存内存中图片
Clipboard.Clear();
}
else
{
MessageBox.Show("抱歉,无法获取当前图片相关信息!");
}
private void SaveBrowserPic(object filePath)
{
FileInfo fi = new FileInfo(filePath.ToString());
byte[] imageData = new byte[fi.Length];
fs = fi.OpenRead();
fs.Read(imageData, 0, Convert.ToInt32(fi.Length));
}
private void SaveBrowserPic2(object image)
{
int resultCount = 0;
int id = 1;
try
{
Bitmap bmp = (Bitmap)image;
//判断内存图片格式
BitmapData bitmapData = bmp.LockBits(new Rectangle(new Point(0, 0), bmp.Size), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte[] tempByte = new byte[2];
IntPtr Ptr = bitmapData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(Ptr, tempByte, 0, tempByte.Length);
bmp.UnlockBits(bitmapData);
string imageType1 = GetExtension(tempByte);
tempByte = null;
bitmapData = null;
//将内存图片转换为字节数组
byte[] imageData = null;
using (MemoryStream ms = new MemoryStream())
{
bmp.Save(ms, GetImageFormat(imageType1));
imageData = new byte[ms.Length];
ms.Position = 0;
ms.Read(imageData, 0, Convert.ToInt32(ms.Length));
}
/// <summary>
/// 判断文件后缀名,只支持gif,bmp,jpg,png
/// </summary>
/// <param name="filePath">图片文件路径</param>
/// <returns>后缀名</returns>
private static string GetExtension(byte[] image)
{
string extension = ".jpg";
string fileclass = string.Empty;
byte buffer;
try
{
buffer = image[0];
fileclass = buffer.ToString();
buffer = image[1];
fileclass += buffer.ToString();
}
catch
{
return extension;
}
string[] fileType = { "1029", "7173", "255216", "6677", "13780" };
string[] type = { ".jfif", ".gif", ".jpg", ".bmp", "png" };
for (int i = 0; i < fileType.Length; i++)
{
if (fileclass == fileType[i])
{
extension = type[i];
break;
}
}
return extension;
}