private void Init() { TextFormat = "<font face=\"Verdana\">"; DefaultBackColor = true; IntelligentTextColor = true; HideText = true; currentOPP.orientation = DominoProvider.FieldPlanDirection; currentOPP.mirrorHorizontal = DominoProvider.FieldPlanDirection == Core.Orientation.Vertical; CurrentProtocol = DominoProvider.GetHTMLProcotol(currentOPP); NaturalOrientation = GetNaturalOrientation(); SkiaSharp.SKImage new_img = DominoProvider.Last.GenerateImage(1000, false).Snapshot(); CurrentPlan = Bitmap.DecodeToWidth(new_img.Encode().AsStream(), new_img.Width); ShowLiveBuildHelper = new RelayCommand(o => { ShowLiveHelper(); }); SaveHTML = new RelayCommand(o => { SaveHTMLFile(); }); SaveExcel = new RelayCommand(o => { SaveExcelFile(); }); this.PropertyChanged += ProtocolVM_PropertyChanged; ClickTopLeft = new RelayCommand(o => SetOrientation(false, false)); ClipTopRight = new RelayCommand(o => SetOrientation(true, false)); ClickBottomLeft = new RelayCommand(o => SetOrientation(false, true)); ClickBottomRight = new RelayCommand(o => SetOrientation(true, true)); // Special case: diagonal fields. We'll rotate the image so the arrows are correct again. if (DominoProvider is StructureParameters structure && XElement.Parse(structure._structureDefinitionXML).Attribute("Name").Value == "Diagonal Field") { RotateAngle = 45; } }
public static Image LoadImage(byte[] pb) { if (pb == null) { throw new ArgumentNullException("pb"); } #if !KeePassLibSD && !NETSTANDARD2_0 // First try to load the data as ICO and afterwards as // normal image, because trying to load an ICO using // the normal image loading methods can result in a // low resolution image try { Image imgIco = ExtractBestImageFromIco(pb); if (imgIco != null) { return(imgIco); } } catch (Exception) { Debug.Assert(false); } #endif MemoryStream ms = new MemoryStream(pb, false); try { return(LoadImagePriv(ms)); } finally { ms.Close(); } }
internal static string ImageToDataUri(Image img) { if (img == null) { Debug.Assert(false); return(string.Empty); } byte[] pb = null; using (MemoryStream ms = new MemoryStream()) { #if NETSTANDARD2_0 var encoded = img.Encode(); // get a stream over the encoded data Stream stream = encoded.AsStream(); stream.CopyTo(ms); //img.Save(Splat.CompressedBitmapFormat.Png, 80, ms); #else img.Save(ms, ImageFormat.Png); #endif pb = ms.ToArray(); } return(StrUtil.DataToDataUri(pb, "image/png")); }
/// <summary> /// Resize an image. /// </summary> /// <param name="img">Image to resize.</param> /// <param name="w">Width of the returned image.</param> /// <param name="h">Height of the returned image.</param> /// <param name="f">Flags to customize scaling behavior.</param> /// <returns>Resized image. This object is always different /// from <paramref name="img" /> (i.e. they can be /// disposed separately).</returns> public static Image ScaleImage(Image img, int w, int h, ScaleTransformFlags f) { if (img == null) { throw new ArgumentNullException("img"); } if (w < 0) { throw new ArgumentOutOfRangeException("w"); } if (h < 0) { throw new ArgumentOutOfRangeException("h"); } bool bUIIcon = ((f & ScaleTransformFlags.UIIcon) != ScaleTransformFlags.None); // We must return a Bitmap object for UIUtil.CreateScaledImage Bitmap bmp = new Bitmap(w, h, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmp)) { g.Clear(Color.Transparent); g.SmoothingMode = SmoothingMode.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality; int wSrc = img.Width; int hSrc = img.Height; InterpolationMode im = InterpolationMode.HighQualityBicubic; if ((wSrc > 0) && (hSrc > 0)) { if (bUIIcon && ((w % wSrc) == 0) && ((h % hSrc) == 0)) { im = InterpolationMode.NearestNeighbor; } // else if((w < wSrc) && (h < hSrc)) // im = InterpolationMode.HighQualityBilinear; } else { Debug.Assert(false); } g.InterpolationMode = im; RectangleF rSource = new RectangleF(0.0f, 0.0f, wSrc, hSrc); RectangleF rDest = new RectangleF(0.0f, 0.0f, w, h); AdjustScaleRects(ref rSource, ref rDest); g.DrawImage(img, rDest, rSource, GraphicsUnit.Pixel); } return(bmp); }
private static Image LoadImagePriv(Stream s) { // Image.FromStream wants the stream to be open during // the whole lifetime of the image; as we can't guarantee // this, we make a copy of the image Image imgSrc = null; try { #if NETSTANDARD2_0 using (var managedStream = new SkiaSharp.SKManagedStream(s)) { // SkiaSharp.SKPixmap pxMap = new SkiaSharp.SKPixmap() using (var original = SkiaSharp.SKBitmap.Decode(managedStream)) { return(SkiaSharp.SKImage.FromBitmap(original)); } } #elif !KeePassLibSD imgSrc = Image.FromStream(s); NormalizeOrientation(imgSrc); Bitmap bmp = new Bitmap(imgSrc.Width, imgSrc.Height, PixelFormat.Format32bppArgb); try { bmp.SetResolution(imgSrc.HorizontalResolution, imgSrc.VerticalResolution); Debug.Assert(bmp.Size == imgSrc.Size); } catch (Exception) { Debug.Assert(false); } #else imgSrc = new Bitmap(s); Bitmap bmp = new Bitmap(imgSrc.Width, imgSrc.Height); using (Graphics g = Graphics.FromImage(bmp)) { g.Clear(Color.Transparent); #if !KeePassLibSD g.DrawImageUnscaled(imgSrc, 0, 0); #else g.DrawImage(imgSrc, 0, 0); #endif } return(bmp); #endif } finally { if (imgSrc != null) { imgSrc.Dispose(); } } }
public static Image LoadImage(byte[] pb) { if (pb == null) { throw new ArgumentNullException("pb"); } MemoryStream ms = new MemoryStream(pb, false); try { return(Image.FromStream(ms)); } finally { ms.Close(); } }
/// <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())); }
internal static ulong HashImage64(Image img) { Bitmap bmp = (img as Bitmap); if (bmp == null) { Debug.Assert(false); return(0); } BitmapData bd = null; try { int w = bmp.Width, h = bmp.Height; if ((w <= 0) || (h <= 0)) { Debug.Assert(false); return(0); } bd = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); if (bd.Stride != (w * 4)) { Debug.Assert(false); return(0); } Debug.Assert(Marshal.SizeOf(typeof(int)) == 4); int cp = w * h; int[] v = new int[cp + 2]; Marshal.Copy(bd.Scan0, v, 0, cp); v[cp] = w; v[cp + 1] = h; return(MemUtil.Hash64(v, 0, v.Length)); } catch (Exception) { Debug.Assert(false); } finally { if (bd != null) { bmp.UnlockBits(bd); } } return(0); }
public static Image ScaleTest(Image[] vIcons) { Bitmap bmp = new Bitmap(1024, vIcons.Length * (256 + 12), PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmp)) { g.Clear(Color.White); int[] v = new int[] { 16, 24, 32, 48, 64, 128, 256 }; int x; int y = 8; foreach (Image imgIcon in vIcons) { if (imgIcon == null) { Debug.Assert(false); continue; } x = 128; foreach (int q in v) { using (Image img = ScaleImage(imgIcon, q, q, ScaleTransformFlags.UIIcon)) { g.DrawImageUnscaled(img, x, y); } x += q + 8; } y += v[v.Length - 1] + 8; } } return(bmp); }
public static Image ScaleImage(Image img, int w, int h) { return(ScaleImage(img, w, h, ScaleTransformFlags.None)); }
private static Image ExtractBestImageFromIco(byte[] pb) { List <GfxImage> l = UnpackIco(pb); if ((l == null) || (l.Count == 0)) { return(null); } long qMax = 0; foreach (GfxImage gi in l) { if (gi.Width == 0) { gi.Width = 256; } if (gi.Height == 0) { gi.Height = 256; } qMax = Math.Max(qMax, (long)gi.Width * (long)gi.Height); } byte[] pbHdrPng = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }; byte[] pbHdrJpeg = new byte[] { 0xFF, 0xD8, 0xFF }; Image imgBest = null; int bppBest = -1; foreach (GfxImage gi in l) { if (((long)gi.Width * (long)gi.Height) < qMax) { continue; } byte[] pbImg = gi.Data; Image img = null; try { if ((pbImg.Length > pbHdrPng.Length) && MemUtil.ArraysEqual(pbHdrPng, MemUtil.Mid <byte>(pbImg, 0, pbHdrPng.Length))) { img = GfxUtil.LoadImage(pbImg); } else if ((pbImg.Length > pbHdrJpeg.Length) && MemUtil.ArraysEqual(pbHdrJpeg, MemUtil.Mid <byte>(pbImg, 0, pbHdrJpeg.Length))) { img = GfxUtil.LoadImage(pbImg); } else { using (MemoryStream ms = new MemoryStream(pb, false)) { using (Icon ico = new Icon(ms, gi.Width, gi.Height)) { img = ico.ToBitmap(); } } } } catch (Exception) { Debug.Assert(false); } if (img == null) { continue; } if ((img.Width < gi.Width) || (img.Height < gi.Height)) { Debug.Assert(false); img.Dispose(); continue; } int bpp = GetBitsPerPixel(img.PixelFormat); if (bpp > bppBest) { if (imgBest != null) { imgBest.Dispose(); } imgBest = img; bppBest = bpp; } else { img.Dispose(); } } return(imgBest); }
private static void NormalizeOrientation(Image img) { if (img == null) { Debug.Assert(false); return; } try { int[] v = img.PropertyIdList; if (v == null) { Debug.Assert(false); return; } if (Array.IndexOf <int>(v, ExifOrientation) < 0) { return; } PropertyItem pi = img.GetPropertyItem(ExifOrientation); if (pi == null) { Debug.Assert(false); return; } if (pi.Type != ExifTypeUInt16) { Debug.Assert(false); return; } byte[] pb = pi.Value; if (pb == null) { Debug.Assert(false); return; } if (pb.Length != 2) { Debug.Assert(false); return; } // Exif supports both LE and BE; use arch.-dep. BitConverter ushort u = BitConverter.ToUInt16(pb, 0); bool bRemoveProp = true; switch (u) { case ExifOrientationTL: bRemoveProp = false; break; case ExifOrientationTR: img.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case ExifOrientationBR: img.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case ExifOrientationBL: img.RotateFlip(RotateFlipType.RotateNoneFlipY); break; case ExifOrientationLT: img.RotateFlip(RotateFlipType.Rotate90FlipX); break; case ExifOrientationRT: img.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case ExifOrientationRB: img.RotateFlip(RotateFlipType.Rotate90FlipY); break; case ExifOrientationLB: img.RotateFlip(RotateFlipType.Rotate270FlipNone); break; default: Debug.Assert(false); bRemoveProp = false; break; } if (bRemoveProp) { img.RemovePropertyItem(ExifOrientation); } } catch (Exception) { Debug.Assert(false); } }
/// <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); }
public void UpdateImage(byte[] image) { SKImageSource = SkiaSharp.SKImage.FromEncodedData(image); OnPropertyChanged("SKImageSource"); }
public void UpdateImage(SkiaSharp.SKImage image) { SKImageSource = image; OnPropertyChanged("SKImageSource"); }