JeffreySu/WeiXinMPSDK · error · ArgumentException
商家名片会员图片仅支持 JPG、JPEG、BMP 或 PNG。
Error message
商家名片会员图片仅支持 JPG、JPEG、BMP 或 PNG。
What it means
WeChat's brand member card image upload only accepts JPG, JPEG, BMP, and PNG images. ValidateBrandMemberImageFileName inspects the fileName extension (case-insensitive) and throws ArgumentException for any other extension, preventing a guaranteed-to-fail upload.
Solutions
- Convert the image to JPG or PNG before upload (e.g. via SkiaSharp/ImageSharp).
- Validate and normalize the extension in your own pipeline before calling the API.
- Rename the file to include a supported extension only when the actual format matches — never just rename a GIF to .png.
Example fix
// before await apis.UploadMemberImageAsync(brandId, bytes, "avatar.webp"); // after var pngBytes = ImageConversion.ConvertToPng(bytes); // transcode first await apis.UploadMemberImageAsync(brandId, pngBytes, "avatar.png");
Defensive patterns
Strategy: validation
Validate before calling
var ext = Path.GetExtension(fileName)?.ToLowerInvariant();
if (ext is not (".jpg" or ".jpeg" or ".bmp" or ".png"))
throw new ArgumentException("Member images must be JPG, JPEG, BMP or PNG."); Type guard
bool supportedImage(string? f) =>
Path.GetExtension(f ?? "")?.ToLowerInvariant() is ".jpg" or ".jpeg" or ".bmp" or ".png"; Try / catch
try { await apis.UploadMemberImageAsync(brandId, bytes, fileName); }
catch (ArgumentException ex) when (ex.Message.Contains("JPG")) { return ConvertToJpg(bytes); } Prevention
- Transcode uploads (WebP/HEIC) to JPG/PNG server-side before any WeChat upload
- Restrict the file picker accept attribute to .jpg,.jpeg,.bmp,.png
- Validate the extension early in the upload pipeline
When it happens
Trigger: Calling UploadMemberImageAsync with a file name whose extension is .gif, .webp, .heic, .tiff, or has no extension at all.
Common situations: Users uploading modern formats (WebP/HEIC) from phones; server-side transcoding pipelines passing original names; file names generated without extensions.
Related errors
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/6d2d19cb9222ef5b.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/BrandMemberCard/BrandMemberCardApis.cs:374
}
private static void ValidateBrandMemberImageFileName(string fileName)
{
if (string.IsNullOrWhiteSpace(fileName))
{
throw new ArgumentException("文件名不能为空。",
nameof(fileName));
}
switch (Path.GetExtension(fileName)?.ToLowerInvariant())
{
case ".jpg":
case ".jpeg":
case ".bmp":
case ".png":
return;
default:
throw new ArgumentException(
"商家名片会员图片仅支持 JPG、JPEG、BMP 或 PNG。",
nameof(fileName));
}
}
private Task<T> PostAsync<T>(string path, object data, int timeOut)
where T : ReturnJsonBase, new()
{
return _request.RequestAsync<T>(GetUrl(path), data, timeOut);
}
private Task<T> PatchAsync<T>(string path, object data, int timeOut)
where T : ReturnJsonBase, new()
{
return _request.RequestAsync<T>(GetUrl(path), data, timeOut,
ApiRequestMethod.PATCH);
}
View on GitHub (pinned to be573f6f94)