订单图片不显示问题修复 读单拍单卡死问题修复

This commit is contained in:
wule committed 2026-08-04 22:52:02 +08:00
1 parent fb4bafd4d8
commit 4a7af051b2
13 files changed
+295 -120

No files matched your search

+107
View File
@@ -3,7 +3,9 @@ using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
namespace AllBase
@@ -334,5 +336,110 @@ namespace AllBase
DeleteObject(h);
return icon;
}
public static Image LoadBitmapLoadBitmipImageFromUrl(string url)
{
try
{
BitmapImage bi = LoadBitmipImageFromUrl(url);
return BitmapImageToSystemDrawingImage(bi);
}
catch (Exception e)
{
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)
{
BitmapImage bitmap = new BitmapImage();
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);
return null;
}
return bitmap;
}
//public string BitmapImageToBase64(BitmapImage bitmapImage)
//{
// // 创建一个 RenderTargetBitmap 对象来渲染 BitmapImage
// RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(
// (int)bitmapImage.PixelWidth,
// (int)bitmapImage.PixelHeight,
// bitmapImage.DpiX,
// bitmapImage.DpiY,
// System.Windows.Media.PixelFormats.Pbgra32);
// renderTargetBitmap.Render(bitmapImage);
// // 将 RenderTargetBitmap 保存到内存流中
// PngBitmapEncoder encoder = new PngBitmapEncoder();
// encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
// using (MemoryStream memoryStream = new MemoryStream())
// {
// encoder.Save(memoryStream);
// byte[] imageBytes = memoryStream.ToArray();
// string base64String = Convert.ToBase64String(imageBytes);
// return base64String;
// }
//}
}
}