private static void AssertClipColors(bool inside, QuantumType value) { using (IMagickImage image = new MagickImage(Files.InvitationTIF)) { image.Alpha(AlphaOption.Transparent); image.Clip("Pad A", inside); image.Alpha(AlphaOption.Opaque); using (IMagickImage mask = image.GetWriteMask()) { Assert.IsNotNull(mask); Assert.AreEqual(false, mask.HasAlpha); using (IPixelCollection pixels = mask.GetPixels()) { MagickColor pixelA = pixels.GetPixel(0, 0).ToColor(); MagickColor pixelB = pixels.GetPixel(mask.Width - 1, mask.Height - 1).ToColor(); Assert.AreEqual(pixelA, pixelB); Assert.AreEqual(value, pixelA.R); Assert.AreEqual(value, pixelA.G); Assert.AreEqual(value, pixelA.B); MagickColor pixelC = pixels.GetPixel(mask.Width / 2, mask.Height / 2).ToColor(); Assert.AreEqual(Quantum.Max - value, pixelC.R); Assert.AreEqual(Quantum.Max - value, pixelC.G); Assert.AreEqual(Quantum.Max - value, pixelC.B); } } } }
public void ShouldUseTheBackgroundColor() { using (IMagickImage image = new MagickImage(Files.Builtin.Wizard)) { image.Alpha(AlphaOption.Transparent); image.BackgroundColor = new MagickColor("red"); image.Alpha(AlphaOption.Background); image.Alpha(AlphaOption.Off); Assert.IsFalse(image.HasAlpha); ColorAssert.AreEqual(new MagickColor(Quantum.Max, 0, 0), image, 0, 0); } }
private void MakeSemiTranspernt(MagickImage image) { double opacitySetting = Quantum.Max / (Quantum.Max * 0.5); image.Alpha(AlphaOption.Set); image.Evaluate(Channels.Alpha, EvaluateOperator.Min, Quantum.Max / opacitySetting); }
public void ShouldCorrectlyWriteGrayscaleAlphaImage() { using (var input = new MagickImage(Files.Builtin.Wizard)) { input.Quantize(new QuantizeSettings { Colors = 10, ColorSpace = ColorSpace.Gray, }); input.Alpha(AlphaOption.Opaque); using (var memoryStream = new MemoryStream()) { input.Settings.Compression = CompressionMethod.RLE; input.Write(memoryStream, MagickFormat.Psd); memoryStream.Position = 0; using (var output = new MagickImage(memoryStream)) { var distortion = output.Compare(input, ErrorMetric.RootMeanSquared); Assert.InRange(distortion, 0.000, 0.001); } } } }
public void Text(string topText, string bottomText) { var readSettings = new MagickReadSettings { StrokeColor = MagickColors.Black, StrokeWidth = 1.75, FillColor = MagickColors.White, BackgroundColor = MagickColors.Transparent, FontFamily = "Impact", TextGravity = Gravity.North, Width = (int)(image.Width * 0.9), }; readSettings.Height = image.Height / (3 + (image.Height / 300)); image.Alpha(AlphaOption.Opaque); if (topText != "") { using (var top = new MagickImage($"caption:{topText}", readSettings)) { image.Composite(top, (int)(image.Width * 0.05), 0, CompositeOperator.Over); } } if (bottomText != "") { readSettings.TextGravity = Gravity.South; using (var bottom = new MagickImage($"caption:{bottomText}", readSettings)) { image.Composite(bottom, (int)(image.Width * 0.05), image.Height - (int)readSettings.Height, CompositeOperator.Over); } } }
private void RemoveAlpha() { int fileCount = fm.LoadFiles(CurrentPackPath, "png", SearchOption.AllDirectories); for (int i = 0; i < fileCount; i++) { FileInfo fi = fm.fileInfos[i]; DateTime creationTime = fi.CreationTime; DateTime writeTime = fi.LastWriteTime; DateTime accessTime = fi.LastAccessTime; using (MagickImage image = new MagickImage(fi)) { image.Alpha(AlphaOption.Opaque); image.Write(fi, MagickFormat.Png24); fi.CreationTime = creationTime; fi.LastWriteTime = writeTime; fi.LastAccessTime = accessTime; } Dispatcher.BeginInvoke(DispatcherPriority.Normal, new TaskProgressDelegate(() => TaskProgress(i, fileCount))); } Dispatcher.BeginInvoke(DispatcherPriority.Normal, new TaskDoneDelegate(TaskDone)); }
public static void TiledText() { // Dynamic label height using (MagickImageCollection images = new MagickImageCollection()) { // Read the image you want to put a label on. MagickImage logo = new MagickImage("radial-gradient:green-yellow", 400, 175); logo.Lower(8); images.Add(logo); MagickImage label = new MagickImage(); label.BackgroundColor = new MagickColor(); label.Settings.FontPointsize = 70; label.Read("caption:Magick.NET is coming for you tinight"); label.Scale(logo.Width, logo.Height); // Resize the label to the width of the image and place the text in the center label.Extent(logo.Width, logo.Height, Gravity.Center); images.Add(label); // Append the images using (MagickImage output = images.Flatten()) { output.Alpha(AlphaOption.Set); output.Transparent(new MagickColor(System.Drawing.Color.White)); output.Blur(0, 8); output.Threshold((Percentage)50); ///output.Polaroid("", 2, PixelInterpolateMethod.Bilinear); output.Write("output-b.png"); } } }
public static async Task BlendImages(string img1Path, string img2Path, string[] imgOutPaths) { try { MagickImage img1 = new MagickImage(img1Path); MagickImage img2 = new MagickImage(img2Path); int alphaFraction = (100f / (imgOutPaths.Length + 1)).RoundToInt(); // Alpha percentage per image int currentAlpha = alphaFraction; foreach (string imgOutPath in imgOutPaths) { string outPath = imgOutPath.Trim(); MagickImage img1Inst = new MagickImage(img1); MagickImage img2Inst = new MagickImage(img2); img2Inst.Alpha(AlphaOption.Opaque); img2Inst.Evaluate(Channels.Alpha, EvaluateOperator.Set, new Percentage(currentAlpha)); currentAlpha += alphaFraction; img1Inst.Composite(img2Inst, Gravity.Center, CompositeOperator.Over); img1Inst.Format = MagickFormat.Png24; img1Inst.Quality = 10; img1Inst.Write(outPath); await Task.Delay(1); } } catch (Exception e) { Logger.Log("BlendImages Error: " + e.Message); } }
private static Stream createWelcomeBannerWithText(String text, MagickImage background) { var textOptions = new MagickReadSettings() { Font = "fonts/font.ttf", StrokeWidth = 2, FontWeight = FontWeight.Bold, FillColor = MagickColors.White, StrokeColor = MagickColors.Black, TextGravity = Gravity.Center, BackgroundColor = MagickColors.Transparent, Width = background.Width, Height = background.Height }; background.Alpha(AlphaOption.Opaque); byte[] imageBytes; using (var label = new MagickImage($"label:{text}", textOptions)) { background.Composite(label, 0, 0, CompositeOperator.Over); imageBytes = background.ToByteArray(); } return(new MemoryStream(imageBytes)); }
public void Process() { using (var bg = new MagickImage(MagickColor.FromRgba(255, 255, 255, 255), width, height)) { using (var image = new MagickImage(input)) { if (image.HasAlpha == false) { image.Alpha(AlphaOption.Opaque); } using (var cover = image.Clone()) { MagickGeometry resize = new MagickGeometry(width + "x" + height + "^"); image.Resize(resize); image.Extent(width, height, Gravity.Center); image.Blur(80, 30); bg.Composite(image, 0, 0, CompositeOperator.Over); MagickGeometry centerResize = new MagickGeometry(height * 0.7 + "x" + height * 0.7); cover.Resize(centerResize); using (var coverShadow = cover.Clone()) { coverShadow.BackgroundColor = MagickColor.FromRgba(192, 192, 192, 192); coverShadow.Shadow(50, 50, 10, new Percentage(90)); bg.Composite(coverShadow, Gravity.Center, CompositeOperator.Over); } bg.Composite(cover, Gravity.Center, CompositeOperator.Over); } } bg.Write(output); } }
public char[][] ConvertImageToASCII(string path) { var pathToImage = path; using var image = new MagickImage(pathToImage); image.Alpha(AlphaOption.Transparent); //Separate the alpha layer from the image var result = image.Separate(Channels.Alpha).First(); //Inverse all colors inside the path result.Negate(); //Copy the resulting mask back into the image as new alpha layer image.Composite(result, CompositeOperator.CopyAlpha); var pixels = image.GetPixels(); var acs = new char[image.Height][]; for (int i = 0; i < image.Height; i++) { acs[i] = new char[image.Width]; for (int j = 0; j < image.Width; j++) { var mapIndex = (int)Map(pixels[j, i].ToColor().R, 0, 255, 0, asciiTable.Length - 1); acs[i][j] = asciiTable[mapIndex]; } } return(acs); }
public void ShouldReturnFalseWhenImageIsNotOpaque() { using (var image = new MagickImage(Files.Builtin.Logo)) { image.Alpha(AlphaOption.Transparent); Assert.IsFalse(image.IsOpaque); } }
///<inheritdoc cref="IMagick.MakeTransperent(byte[])"/> public string MakeTransperent(byte[] img) { using (var image = new MagickImage(img)) { image.Format = MagickFormat.Png; image.Alpha(AlphaOption.Set); image.ColorFuzz = new Percentage(10); image.Opaque(MagickColors.White, MagickColor.FromRgba(0, 0, 0, 0)); return(image.ToBase64()); } }
private void CopyOpacity(MagickImage image) { image.Alpha(AlphaOption.Off); using (MagickImage gray = image.Clone()) { gray.ColorSpace = ColorSpace.Gray; gray.Negate(); gray.AdaptiveThreshold(FilterSize, FilterSize, FilterOffset); gray.ContrastStretch((Percentage)0); if (Threshold.HasValue) { gray.Blur((double)Threshold.Value / 100.0, Quantum.Max); gray.Level(Threshold.Value, new Percentage(100)); } image.Composite(gray, CompositeOperator.CopyAlpha); image.Opaque(MagickColor.Transparent, BackgroundColor); image.Alpha(AlphaOption.Off); } }
public void ShouldMakeImageTransparent() { using (var image = new MagickImage(Files.Builtin.Wizard)) { Assert.False(image.HasAlpha); image.Alpha(AlphaOption.Transparent); Assert.True(image.HasAlpha); ColorAssert.Equal(new MagickColor("#fff0"), image, 0, 0); } }
public void Compression_NotSetForImageWithAlphaChannel_CompressionMethodIsDXT5() { using (IMagickImage input = new MagickImage(Files.Builtin.Logo)) { input.Alpha(AlphaOption.Set); using (IMagickImage output = WriteDds(input)) { Assert.AreEqual(Compression.DXT5, output.Compression); } } }
public void ShouldUseDxt5AsTheDefaultCompressionForImagesWithAnAlphaChannel() { using (IMagickImage input = new MagickImage(Files.Builtin.Logo)) { input.Alpha(AlphaOption.Set); using (IMagickImage output = WriteDds(input)) { Assert.AreEqual(CompressionMethod.DXT5, output.Compression); } } }
public void ShouldMakeImageTransparent() { using (IMagickImage image = new MagickImage(Files.Builtin.Wizard)) { Assert.AreEqual(image.HasAlpha, false); image.Alpha(AlphaOption.Transparent); Assert.AreEqual(image.HasAlpha, true); ColorAssert.AreEqual(MagickColors.Transparent, image, 0, 0); } }
public static void BlendImages(string img1Path, string img2Path, string imgOutPath) { MagickImage img1 = new MagickImage(img1Path); MagickImage img2 = new MagickImage(img2Path); img2.Alpha(AlphaOption.Opaque); img2.Evaluate(Channels.Alpha, EvaluateOperator.Set, new Percentage(50)); img1.Composite(img2, Gravity.Center, CompositeOperator.Over); img1.Format = MagickFormat.Png24; img1.Quality = 10; img1.Write(imgOutPath); }
private void DrawShaded(MagickImage overlay, DrawableFixture fixture) { //MainForm.Log(string.Format("Shaded: {0} ({1}) ...", fixture.Name, fixture.NifName), MainForm.LogLevel.notice); using (MagickImage modelCanvas = new MagickImage(MagickColors.Transparent, fixture.CanvasWidth, fixture.CanvasHeight)) { foreach (DrawableElement drawableElement in fixture.DrawableElements) { // A Shaded model without lightning is not shaded... but just we add this just be flexible if (fixture.RendererConf.HasLight) { modelCanvas.Settings.FillColor = new MagickColor( Convert.ToUInt16(drawableElement.lightning * fixture.RendererConf.Color.R), Convert.ToUInt16(drawableElement.lightning * fixture.RendererConf.Color.G), Convert.ToUInt16(drawableElement.lightning * fixture.RendererConf.Color.B) ); } else { modelCanvas.Settings.FillColor = fixture.RendererConf.Color; } DrawablePolygon polyDraw = new DrawablePolygon(drawableElement.coordinates); modelCanvas.Draw(polyDraw); } if (fixture.RendererConf.HasShadow) { CastShadow( modelCanvas, fixture.RendererConf.ShadowOffsetX, fixture.RendererConf.ShadowOffsetY, fixture.RendererConf.ShadowSize, new Percentage(100 - fixture.RendererConf.ShadowTransparency), fixture.RendererConf.ShadowColor ); // Update the canvas position to match the new border fixture.CanvasX -= fixture.RendererConf.ShadowSize; fixture.CanvasY -= fixture.RendererConf.ShadowSize; } if (fixture.RendererConf.Transparency != 0) { modelCanvas.Alpha(AlphaOption.Set); double divideValue = 100.0 / (100.0 - fixture.RendererConf.Transparency); modelCanvas.Evaluate(Channels.Alpha, EvaluateOperator.Divide, divideValue); } overlay.Composite(modelCanvas, Convert.ToInt32(fixture.CanvasX), Convert.ToInt32(fixture.CanvasY), CompositeOperator.SrcOver); } }
public MagicCanvas(int width, int height, bool noAlpha = false) { var backColor = noAlpha ? MagickColors.Black : MagickColors.Transparent ; bitmap = new MagickImage(backColor, width, height); if (!noAlpha) { bitmap.ColorType = ColorType.TrueColorAlpha; bitmap.Alpha(AlphaOption.Transparent); bitmap.ColorAlpha(MagickColors.Transparent); } }
private void ScaleImage(string input, string output, int width, int height) { using (var image = new MagickImage(input)) { var size = new MagickGeometry(width, height); log.Info($"Resizing {input} to {output}"); size.IgnoreAspectRatio = true; image.Resize(size); image.Depth = 8; image.Settings.Compression = ImageMagick.CompressionMethod.RLE; image.Settings.Format = MagickFormat.Bmp3; image.ColorType = ColorType.Palette; image.Alpha(AlphaOption.Off); image.Write(output); } }
public void ShouldUseDxt1CompressionWhenSetToDxt1AndImageHasAlphaChannel() { using (var input = new MagickImage(Files.Builtin.Logo)) { input.Alpha(AlphaOption.Set); input.Settings.SetDefines(new DdsWriteDefines { Compression = DdsCompression.Dxt1, }); using (var output = WriteDds(input)) { Assert.Equal(CompressionMethod.DXT1, output.Compression); } } }
public void Compression_SetToDxt1ForImageWithAlphaChannel_CompressionMethodIsDxt1() { using (IMagickImage input = new MagickImage(Files.Builtin.Logo)) { input.Alpha(AlphaOption.Set); input.Settings.SetDefines(new DdsWriteDefines() { Compression = DdsCompression.Dxt1, }); using (IMagickImage output = WriteDds(input)) { Assert.AreEqual(Compression.DXT1, output.Compression); } } }
public void ShouldSetTheDefine() { using (var input = new MagickImage(Files.Builtin.Logo)) { input.Settings.SetDefines(new TiffWriteDefines { Alpha = TiffAlpha.Associated, }); input.Alpha(AlphaOption.Set); using (var output = WriteTiff(input)) { Assert.Equal("associated", output.GetAttribute("tiff:alpha")); } } }
public MagickImage PlasmaBackground(string color1 = "steelblue", string color2 = "black", int width = 1024, int height = 768) { string plasmaImageText = "plasma:"; plasmaImageText += color1 + "-"; plasmaImageText += color2; var plasmaImage = new MagickImage(plasmaImageText, width, height); plasmaImage.Alpha(AlphaOption.Set); plasmaImage.BackgroundColor = MagickColors.Transparent; //plasmaImage.Vignette(0.5, 100, width, height); return(plasmaImage); }
public void ShouldBeAbleToConvertRgbaImage() { using (var image = new MagickImage(ToMagickColor(Color.Magenta), 5, 1)) { image.Alpha(AlphaOption.On); using (var bitmap = image.ToBitmapWithDensity()) { var color = MagickColors.Magenta; color.A = Quantum.Max; for (int i = 0; i < image.Width; i++) { ColorAssert.Equal(color, ToMagickColor(bitmap.GetPixel(i, 0))); } } } }
///<inheritdoc cref="IMagick.MakeCircle(byte[])"/> public string MakeCircle(byte[] img) { using (var image = new MagickImage(img)) { image.Format = MagickFormat.Png; image.Alpha(AlphaOption.Set); using (MagickImage copy = new MagickImage(image.Clone())) { copy.Distort(DistortMethod.DePolar, 0); copy.VirtualPixelMethod = VirtualPixelMethod.HorizontalTile; copy.BackgroundColor = MagickColors.None; copy.Distort(DistortMethod.Polar, 0); image.Compose = CompositeOperator.DstIn; image.Composite(copy, CompositeOperator.CopyAlpha); } return(image.ToBase64()); } }
/// <summary> /// 指定したパスのサーフェスを読み込み、必要な透過処理を施す /// </summary> public virtual MagickImage LoadAndProcessSurfaceFile(string surfacePath) { // 画像ファイル読み込み var surface = new MagickImage(surfacePath); // seriko.use_self_alpha が1、かつアルファチャンネルありの画像の場合は、元画像をそのまま返す if (SerikoUseSelfAlpha && surface.HasAlpha) { return(surface); } else { // pnaが存在するかどうかをチェック var pnaPath = Path.ChangeExtension(surfacePath, ".pna"); if (File.Exists(pnaPath)) { // pnaありの場合は、PNAによる透過処理 // from <https://dobon.net/vb/dotnet/graphics/drawnegativeimage.html> // pnaマスク画像の読み込み var mask = new MagickImage(pnaPath); // Alpha copy処理を行い、グレースケール画像を透過マスクに変換する // <https://www.imagemagick.org/Usage/masking/#alpha_copy> mask.Alpha(AlphaOption.Copy); // 透過度をサーフェス画像にコピー surface.Composite(mask, CompositeOperator.CopyAlpha); return(surface); } else { // pnaなしの場合は、画像の左上の色を透過色として設定する var pixels = surface.GetPixels(); var basePixel = pixels.GetPixel(0, 0); surface.Transparent(basePixel.ToColor()); // 32ビットRGBA画像を強制 surface.Alpha(AlphaOption.Set); return(surface); } } }
internal static MagickImage ProcessImage(Stream image, bool disposeStream = true) { var magickImage = new MagickImage(image); magickImage.Alpha(AlphaOption.Remove); magickImage.BackgroundColor = MagickColor.FromRgb(255, 255, 255); if (magickImage.DetermineColorType() == ColorType.Grayscale) { magickImage.Grayscale(); } if (disposeStream) { image.Dispose(); } return(magickImage); }