Skip to content

Commit

Permalink
ImageSouce
Browse files Browse the repository at this point in the history
  • Loading branch information
AigioL committed Sep 4, 2021
1 parent af61df6 commit d783dcd
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public class BitmapAssetValueConverter : ImageValueConverter
TryReset(s);
return GetDecodeBitmap(s, width);
}
else if (value is ImageClipStream ics)
else if (value is ImageSouce.ClipStream clipStream)
{
return GetBitmap(ics);
return GetBitmap(clipStream);
}
else if (value is Guid imageid)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,25 @@ protected static void TryReset(Stream s)
}
}

protected static Bitmap? GetBitmap(ImageClipStream ics, int width = 0)
protected static Bitmap? GetBitmap(ImageSouce.ClipStream clipStream, int width = 0)
{
if (ics.Stream == null)
if (clipStream.Stream == null)
return null;
TryReset(ics.Stream);
TryReset(clipStream.Stream);
using var ms = new MemoryStream();
ics.Stream.CopyTo(ms);
clipStream.Stream.CopyTo(ms);
TryReset(ms);
using var bitmapSource = SKBitmap.Decode(ms);
using var bitmapDest = new SKBitmap(bitmapSource.Width, bitmapSource.Height, SKColorType.Bgra8888, SKAlphaType.Unpremul);

using var canvas = new SKCanvas(bitmapDest);

var rect = ics.Circle ?
var rect = clipStream.Circle ?
new SKRect(0, 0, bitmapSource.Width, bitmapSource.Height) :
new SKRect(ics.Left, ics.Top, ics.Right, ics.Bottom);
var roundRect = ics.Circle ?
new SKRect(clipStream.Left, clipStream.Top, clipStream.Right, clipStream.Bottom);
var roundRect = clipStream.Circle ?
new SKRoundRect(rect, bitmapSource.Width / 2f, bitmapSource.Height / 2f) :
new SKRoundRect(rect, ics.Radius_X, ics.Radius_Y);
new SKRoundRect(rect, clipStream.Radius_X, clipStream.Radius_Y);
canvas.ClipRoundRect(roundRect, antialias: true);

canvas.DrawBitmap(bitmapSource, 0, 0);
Expand Down
123 changes: 123 additions & 0 deletions src/ST.Client.Desktop/ImageSouce.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;

namespace System.Application
{
public static class ImageSouce
{
public sealed class ClipStream : IDisposable
{
bool disposedValue;

ClipStream(Stream stream)
{
Stream = stream;
}

public Stream Stream { get; init; }

public float Top { get; set; }

public float Left { get; set; }

public float Right { get; set; }

public float Bottom { get; set; }

public float TopBottom
{
set
{
Top = value;
Bottom = value;
}
}

public float LeftRight
{
set
{
Left = value;
Right = value;
}
}

public float Radius_X { get; set; }

public float Radius_Y { get; set; }

public float Radius
{
set
{
Radius_X = value;
Radius_Y = value;
}
}

public bool Circle { get; set; }

void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: 释放托管状态(托管对象)
Stream.Dispose();
}

// TODO: 释放未托管的资源(未托管的对象)并重写终结器
// TODO: 将大型字段设置为 null
disposedValue = true;
}
}

public void Dispose()
{
// 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

[return: NotNullIfNotNull("stream")]
public static implicit operator ClipStream?(Stream? stream) => stream == null ? null : new(stream);

public static implicit operator ClipStream?(string? filePath)
{
if (filePath == null) return null;
try
{
ClipStream? clipStream = IOPath.OpenRead(filePath);
return clipStream;
}
catch
{
return null;
}
}
}

/// <summary>
/// 将图片文件本地路径或 Avalonia 资源路径转为图像源
/// </summary>
/// <param name="filePathOrAvaloniaResPath"></param>
/// <param name="isCircle">是否为圆形</param>
/// <param name="config">是否为圆形</param>
/// <returns></returns>
public static object? TryParse(string? filePathOrAvaloniaResPath, bool isCircle = false, Action<ClipStream>? config = null)
{
if (filePathOrAvaloniaResPath == null)
return null;
if (filePathOrAvaloniaResPath.StartsWith("avares:"))
return filePathOrAvaloniaResPath;
ClipStream? clipStream = filePathOrAvaloniaResPath;
if (clipStream != null)
{
clipStream.Circle = isCircle;
config?.Invoke(clipStream);
}
return clipStream;
}
}
}
136 changes: 0 additions & 136 deletions src/ST.Client.Desktop/Models/ImageClipStream.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/ST.Client.Desktop/Services/Mvvm/SteamConnectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public void Initialize()
IsConnectToSteam = true;
CurrentSteamUser = await DI.Get<ISteamworksWebApiService>().GetUserInfo(id);
CurrentSteamUser.AvatarStream = IHttpService.Instance.GetImageAsync(CurrentSteamUser.AvatarFull, ImageChannelType.SteamAvatars);
AvaterPath = CircleImageStream.TryConvert(await CurrentSteamUser.AvatarStream);
AvaterPath = ImageSouce.TryParse(await CurrentSteamUser.AvatarStream, isCircle: true);

CurrentSteamUser.IPCountry = ApiService.GetIPCountry();
IsSteamChinaLauncher = ApiService.IsSteamChinaLauncher();
Expand Down
7 changes: 4 additions & 3 deletions src/ST.Client.Desktop/Services/Mvvm/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ public async void RefreshUserAvaterAsync()
if (!string.IsNullOrWhiteSpace(avatarUrl))
{
var avatarLocalFilePath = await IHttpService.Instance.GetImageAsync(avatarUrl, ImageChannelType.SteamAvatars);
AvaterPath = CircleImageStream.TryConvert(avatarLocalFilePath);
var avaterSouce = ImageSouce.TryParse(avatarLocalFilePath, isCircle: true);
AvaterPath = avaterSouce ?? DefaultAvaterPath;
}
return;
}
Expand All @@ -260,8 +261,8 @@ async Task<bool> RefreshSteamUserAvaterAsync()
{
CurrentSteamUser = await ISteamworksWebApiService.Instance.GetUserInfo(User.SteamAccountId.Value);
CurrentSteamUser.AvatarStream = IHttpService.Instance.GetImageAsync(CurrentSteamUser.AvatarFull, ImageChannelType.SteamAvatars);
var avaterStream = CircleImageStream.TryConvert(await CurrentSteamUser.AvatarStream);
AvaterPath = avaterStream ?? DefaultAvaterPath;
var avaterSouce = ImageSouce.TryParse(await CurrentSteamUser.AvatarStream, isCircle: true);
AvaterPath = avaterSouce ?? DefaultAvaterPath;
return true;
}
else
Expand Down

0 comments on commit d783dcd

Please sign in to comment.