Browse Source
Merge pull request #1153 from SixLabors/js/more-efficient-base64
More Efficient ToBase64String
pull/1155/head
James Jackson-South
6 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with
6 additions and
6 deletions
-
src/ImageSharp/ImageExtensions.cs
|
|
|
@ -112,12 +112,12 @@ namespace SixLabors.ImageSharp |
|
|
|
public static string ToBase64String<TPixel>(this Image<TPixel> source, IImageFormat format) |
|
|
|
where TPixel : unmanaged, IPixel<TPixel> |
|
|
|
{ |
|
|
|
using (var stream = new MemoryStream()) |
|
|
|
{ |
|
|
|
source.Save(stream, format); |
|
|
|
stream.Flush(); |
|
|
|
return $"data:{format.DefaultMimeType};base64,{Convert.ToBase64String(stream.ToArray())}"; |
|
|
|
} |
|
|
|
using var stream = new MemoryStream(); |
|
|
|
source.Save(stream, format); |
|
|
|
|
|
|
|
// Always available.
|
|
|
|
stream.TryGetBuffer(out ArraySegment<byte> buffer); |
|
|
|
return $"data:{format.DefaultMimeType};base64,{Convert.ToBase64String(buffer.Array, 0, (int)stream.Length)}"; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|