微信商品图片无法显示/上传问题优化(30%)
This commit is contained in:
1 parent
b7241236ca
commit
1aa0954e43
7 files changed
+175
-34
No files matched your search
@@ -5,7 +5,9 @@ using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace AllBase
|
||||
{
|
||||
@@ -587,6 +589,89 @@ namespace AllBase
|
||||
fs.Close();
|
||||
return imgBytes;
|
||||
}
|
||||
|
||||
public static Image LoadBitmapLoadBitmipImageFromUrl(string url, out string error)
|
||||
{
|
||||
try
|
||||
{
|
||||
BitmapImage bi = LoadBitmipImageFromUrl(url, out error);
|
||||
return BitmapImageToSystemDrawingImage(bi);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
error = e.Message;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Image BitmapImageToSystemDrawingImage(BitmapImage bitmapImage)
|
||||
{
|
||||
// 方法1 会乱码花屏
|
||||
//int width = bitmapImage.PixelWidth;
|
||||
//int height = bitmapImage.PixelHeight;
|
||||
//int stride = width * ((bitmapImage.Format.BitsPerPixel + 7) / 8);
|
||||
|
||||
//byte[] pixels = new byte[stride * height];
|
||||
//bitmapImage.CopyPixels(pixels, stride, 0);
|
||||
|
||||
//Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
|
||||
//BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, bitmap.PixelFormat);
|
||||
|
||||
//Marshal.Copy(pixels, 0, bmpData.Scan0, pixels.Length);
|
||||
//bitmap.UnlockBits(bmpData);
|
||||
|
||||
//return bitmap;
|
||||
|
||||
// 方法2
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
BitmapEncoder encoder = new BmpBitmapEncoder();
|
||||
encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
|
||||
encoder.Save(stream);
|
||||
stream.Position = 0;
|
||||
Bitmap bitmap = new Bitmap(stream);
|
||||
stream.Close();
|
||||
|
||||
//// 现在你可以将这个Bitmap对象用于Windows Forms的PictureBox或其他图像显示控件
|
||||
//PictureBox pictureBox = new PictureBox();
|
||||
//pictureBox.Image = bitmap; // 或者如果你已经有了一个PictureBox控件,直接设置其Image属性
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
|
||||
public static BitmapImage LoadBitmipImageFromUrl(string url, out string error)
|
||||
{
|
||||
BitmapImage bitmap = new BitmapImage();
|
||||
error = "";
|
||||
|
||||
try
|
||||
{
|
||||
using (WebClient webClient = new WebClient())
|
||||
{
|
||||
// 下载图片数据到内存流
|
||||
using (MemoryStream stream = new MemoryStream(webClient.DownloadData(url)))
|
||||
{
|
||||
stream.Position = 0; // 重置流的位置到开始处
|
||||
bitmap.BeginInit();
|
||||
bitmap.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
|
||||
bitmap.CacheOption = BitmapCacheOption.OnLoad; // 或者使用 BitmapCacheOption.OnLoad 以减少内存使用
|
||||
bitmap.UriSource = null; // 清除可能的旧UriSource,防止缓存问题
|
||||
bitmap.StreamSource = stream;
|
||||
bitmap.EndInit();
|
||||
bitmap.Freeze(); // 防止后续更改,提高性能
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 处理异常,例如打印错误信息或设置一个默认图片等
|
||||
//Console.WriteLine("Error loading image: " + ex.Message);
|
||||
error = ex.Message;
|
||||
return null;
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
public class ImagePiex
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user