Hello,
Firstly, please make sure this var stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{avatarFileName}");
, stream is not null.
Is stream is not null, please copy stream to MemoryStream, then set the ms.Position = 0;
string avatarFileName = "MyProjectName.Avatars." + selectedAvatar;
var assembly = typeof(RESTServices).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{avatarFileName}");
MemoryStream ms = new MemoryStream();
stream.CopyTo(ms);
ms.Position = 0;
StreamContent streamContent = new StreamContent(ms);
//you can detect the streamContent is not null by the length
var length= streamContent.Headers.ContentLength;
If it is null, please set the image's build action to the MauiAsset
in the Avatars folder, then read it by using var stream = await FileSystem.OpenAppPackageFileAsync("Avatars\\"+ selectedAvatar);
Here is my tested code, you can refer to it.
using var stream = await FileSystem.OpenAppPackageFileAsync("Avatars\\"+ selectedAvatar);
MemoryStream ms = new MemoryStream();
stream.CopyTo(ms);
ms.Position = 0;
StreamContent streamContent = new StreamContent(ms);
//you can detect the streamContent is not null by the length
var length= streamContent.Headers.ContentLength;
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.