public void Save(string filename, SkiaSharp.SKEncodedImageFormat format) { if (Disposed) { return; } SkiaSharp.SKBitmap bitmap = new SkiaSharp.SKBitmap(this.Width, this.Height, false); int w = Width; int h = Height; var colorPtr = this.GetColors(); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { var col = *(colorPtr + x + (h - 1 - y) * h); bitmap.SetPixel(x, y, new SkiaSharp.SKColor(col.R, col.G, col.B, col.A)); } } var img = SkiaSharp.SKImage.FromBitmap(bitmap); var data = img.Encode(format, 100); System.IO.File.WriteAllBytes(filename, data.ToArray()); img.Dispose(); bitmap.Dispose(); }
/// <summary> /// Creates a new header. /// </summary> /// <param name="header">The tag header.</param> /// <param name="flags">The flags.</param> /// <param name="description">The description.</param> /// <param name="type">The type.</param> /// <param name="image">The image.</param> /// <param name="imageFormat">The image format.</param> /// <param name="quality">The quality.</param> /// <returns></returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public static ID3v2APICFrame Create(ID3v2Header header, ID3v2FrameFlags flags, string description, ID3v2PictureType type, SkiaSharp.SKImage image, SkiaSharp.SKEncodedImageFormat imageFormat = SkiaSharp.SKEncodedImageFormat.Jpeg, int quality = 99) { var data = image.Encode(imageFormat, quality); string mimeType; switch (imageFormat) { case SkiaSharp.SKEncodedImageFormat.Jpeg: mimeType = MimeTypes.FromExtension(".jpg"); break; case SkiaSharp.SKEncodedImageFormat.Png: mimeType = MimeTypes.FromExtension(".png"); break; default: throw new ArgumentOutOfRangeException(string.Format("ImageFormat {0} not suppoerted!", imageFormat)); } return(Create(header, flags, description, type, mimeType, data.ToArray())); }
public static bool ToImage(this SkiaSharp.SKPicture skPicture, Stream stream, SkiaSharp.SKColor background, SkiaSharp.SKEncodedImageFormat format, int quality, float scaleX, float scaleY, SkiaSharp.SKColorType skColorType, SkiaSharp.SKAlphaType skAlphaType, SkiaSharp.SKColorSpace skColorSpace) { using var skBitmap = skPicture.ToBitmap(background, scaleX, scaleY, skColorType, skAlphaType, skColorSpace); if (skBitmap is null) { return(false); } using var skImage = SkiaSharp.SKImage.FromBitmap(skBitmap); using var skData = skImage.Encode(format, quality); if (skData is { })
/// <summary> /// 生成二维码(320*320) /// </summary> /// <param name="text">文本内容</param> /// <param name="format">保存格式</param> /// <param name="logoImgae">Logo图片(缩放到真实二维码区域尺寸的1/6)</param> /// <param name="keepWhiteBorderPixelVal">白边处理(负值表示不做处理,最大值不超过真实二维码区域的1/10)</param> /// <returns></returns> public static byte[] QRCoder(string text, SkiaSharp.SKEncodedImageFormat format, byte[] logoImgae = null, int keepWhiteBorderPixelVal = -1) { byte[] reval = null; int width = 320; int height = 320; var qRCodeWriter = new ZXing.QrCode.QRCodeWriter(); var hints = new Dictionary <ZXing.EncodeHintType, object>(); hints.Add(ZXing.EncodeHintType.CHARACTER_SET, "utf-8"); hints.Add(ZXing.EncodeHintType.QR_VERSION, 8); hints.Add(ZXing.EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.Q); var bitMatrix = qRCodeWriter.encode(text, ZXing.BarcodeFormat.QR_CODE, width, height, hints); var w = bitMatrix.Width; var h = bitMatrix.Height; var sKBitmap = new SkiaSharp.SKBitmap(w, h); int blackStartPointX = 0; int blackStartPointY = 0; int blackEndPointX = w; int blackEndPointY = h; #region --绘制二维码(同时获取真实的二维码区域起绘点和结束点的坐标)-- var sKCanvas = new SkiaSharp.SKCanvas(sKBitmap); var sKColorBlack = SkiaSharp.SKColor.Parse("000000"); var sKColorWihte = SkiaSharp.SKColor.Parse("ffffff"); sKCanvas.Clear(sKColorWihte); bool blackStartPointIsNotWriteDown = true; for (var y = 0; y < h; y++) { for (var x = 0; x < w; x++) { var flag = bitMatrix[x, y]; if (flag) { if (blackStartPointIsNotWriteDown) { blackStartPointX = x; blackStartPointY = y; blackStartPointIsNotWriteDown = false; } blackEndPointX = x; blackEndPointY = y; sKCanvas.DrawPoint(x, y, sKColorBlack); } else { //sKCanvas.DrawPoint(x, y, sKColorWihte);//不用绘制(背景是白色的) } } } sKCanvas.Dispose(); #endregion int qrcodeRealWidth = blackEndPointX - blackStartPointX; int qrcodeRealHeight = blackEndPointY - blackStartPointY; #region -- 处理白边 -- if (keepWhiteBorderPixelVal > -1)//指定了边框宽度 { var borderMaxWidth = (int)Math.Floor((double)qrcodeRealWidth / 10); if (keepWhiteBorderPixelVal > borderMaxWidth) { keepWhiteBorderPixelVal = borderMaxWidth; } var nQrcodeRealWidth = width - keepWhiteBorderPixelVal - keepWhiteBorderPixelVal; var nQrcodeRealHeight = height - keepWhiteBorderPixelVal - keepWhiteBorderPixelVal; var sKBitmap2 = new SkiaSharp.SKBitmap(width, height); var sKCanvas2 = new SkiaSharp.SKCanvas(sKBitmap2); sKCanvas2.Clear(sKColorWihte); //二维码绘制到临时画布上时无需抗锯齿等处理(避免文件增大) sKCanvas2.DrawBitmap( sKBitmap, new SkiaSharp.SKRect { Location = new SkiaSharp.SKPoint { X = blackStartPointX, Y = blackStartPointY }, Size = new SkiaSharp.SKSize { Height = qrcodeRealHeight, Width = qrcodeRealWidth } }, new SkiaSharp.SKRect { Location = new SkiaSharp.SKPoint { X = keepWhiteBorderPixelVal, Y = keepWhiteBorderPixelVal }, Size = new SkiaSharp.SKSize { Width = nQrcodeRealWidth, Height = nQrcodeRealHeight } }); blackStartPointX = keepWhiteBorderPixelVal; blackStartPointY = keepWhiteBorderPixelVal; qrcodeRealWidth = nQrcodeRealWidth; qrcodeRealHeight = nQrcodeRealHeight; sKCanvas2.Dispose(); sKBitmap.Dispose(); sKBitmap = sKBitmap2; } #endregion #region -- 绘制LOGO -- if (logoImgae != null && logoImgae.Length > 0) { SkiaSharp.SKBitmap sKBitmapLogo = SkiaSharp.SKBitmap.Decode(logoImgae); if (!sKBitmapLogo.IsEmpty) { var sKPaint2 = new SkiaSharp.SKPaint { FilterQuality = SkiaSharp.SKFilterQuality.None, IsAntialias = true }; var logoTargetMaxWidth = (int)Math.Floor((double)qrcodeRealWidth / 6); var logoTargetMaxHeight = (int)Math.Floor((double)qrcodeRealHeight / 6); var qrcodeCenterX = (int)Math.Floor((double)qrcodeRealWidth / 2); var qrcodeCenterY = (int)Math.Floor((double)qrcodeRealHeight / 2); var logoResultWidth = sKBitmapLogo.Width; var logoResultHeight = sKBitmapLogo.Height; if (logoResultWidth > logoTargetMaxWidth) { var r = (double)logoTargetMaxWidth / logoResultWidth; logoResultWidth = logoTargetMaxWidth; logoResultHeight = (int)Math.Floor(logoResultHeight * r); } if (logoResultHeight > logoTargetMaxHeight) { var r = (double)logoTargetMaxHeight / logoResultHeight; logoResultHeight = logoTargetMaxHeight; logoResultWidth = (int)Math.Floor(logoResultWidth * r); } var pointX = qrcodeCenterX - (int)Math.Floor((double)logoResultWidth / 2) + blackStartPointX; var pointY = qrcodeCenterY - (int)Math.Floor((double)logoResultHeight / 2) + blackStartPointY; var sKCanvas3 = new SkiaSharp.SKCanvas(sKBitmap); var sKPaint = new SkiaSharp.SKPaint { FilterQuality = SkiaSharp.SKFilterQuality.Medium, IsAntialias = true }; sKCanvas3.DrawBitmap( sKBitmapLogo, new SkiaSharp.SKRect { Location = new SkiaSharp.SKPoint { X = 0, Y = 0 }, Size = new SkiaSharp.SKSize { Height = sKBitmapLogo.Height, Width = sKBitmapLogo.Width } }, new SkiaSharp.SKRect { Location = new SkiaSharp.SKPoint { X = pointX, Y = pointY }, Size = new SkiaSharp.SKSize { Height = logoResultHeight, Width = logoResultWidth } }, sKPaint); sKCanvas3.Dispose(); sKPaint.Dispose(); sKBitmapLogo.Dispose(); } else { sKBitmapLogo.Dispose(); } } #endregion SkiaSharp.SKImage sKImage = SkiaSharp.SKImage.FromBitmap(sKBitmap); sKBitmap.Dispose(); var data = sKImage.Encode(format, 75); sKImage.Dispose(); reval = data.ToArray(); data.Dispose(); return(reval); }