微信商品图片无法显示/上传问题优化(30%)

This commit is contained in:
wule committed 2026-08-20 21:09:04 +08:00
1 parent b7241236ca
commit 1aa0954e43
7 files changed
+175 -34

No files matched your search

+2
View File
@@ -128,6 +128,7 @@
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Data.SQLite, Version=1.0.109.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86">
@@ -142,6 +143,7 @@
<Reference Include="System.Web.Services" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="zxing, Version=0.12.0.0, Culture=neutral, PublicKeyToken=4e88037ac681fe60, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\DllReference\zxing.dll</HintPath>
+6
View File
@@ -55,6 +55,12 @@ namespace AllBase
{
rst = "";
//return LoadImage(imgUrl);
if (imgUrl.Contains("wxapp"))
{
return ImageBuild.LoadBitmapLoadBitmipImageFromUrl(imgUrl, out rst);
}
Image image = null;
HttpWebResponse response = null;
try
+85
View File
@@ -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
{
+4 -2
View File
@@ -10,7 +10,7 @@ namespace AllBase.Model.DBModel
{
public long id { get; set; }
/// <summary>
/// 变动类型,1:充值,2:采购花费,3:产品退款到账,4:运费退款到账,5:采购收款,6:产品退款扣除,7:运费退款扣除,16:礼物单话费,20:中转运费,23:作废运费返还
/// 变动类型,1:充值,2:采购花费,3:产品退款到账,4:运费退款到账,5:采购收款,6:产品退款扣除,7:运费退款扣除,16:礼物单话费,20:中转运费,23:作废运费返还 26退货包裹扣费
/// </summary>
public int? type { get; set; }
/// <summary>
@@ -76,10 +76,12 @@ namespace AllBase.Model.DBModel
//采购收款 = 5,
//产品退款扣除 = 6,
//运费退款扣除 = 7,
//礼物单支出 = 16,
礼物单支出 = 16,
中转仓扣费支出 = 20,
中转仓作废返还收入 = 23,
中转仓超重支出 = 24,
退货包裹扣费支出 = 26,
支付宝在线充值 = 30,
云仓退货支出 = 31,
}
}