/// <inheritdoc /> protected override void OnFrameApply(ImageFrame <TPixelBg> source) { SixLabors.ImageSharp.Rectangle sourceRectangle = this.SourceRectangle; Configuration configuration = this.Configuration; SixLabors.ImageSharp.Image <TPixelFg> image = this.Image; PixelBlender <TPixelBg> blender = this.Blender; int y = this.Location.Y; SixLabors.ImageSharp.Rectangle rectangle1 = image.Bounds(); int num = Math.Max(this.Location.X, sourceRectangle.X); int right = Math.Min(this.Location.X + rectangle1.Width, sourceRectangle.Right); int targetX = num - this.Location.X; int top = Math.Max(this.Location.Y, sourceRectangle.Y); int bottom = Math.Min(this.Location.Y + rectangle1.Height, sourceRectangle.Bottom); int width = right - num; SixLabors.ImageSharp.Rectangle rectangle2 = SixLabors.ImageSharp.Rectangle.FromLTRB(num, top, right, bottom); if (rectangle2.Width <= 0 || rectangle2.Height <= 0) { throw new ImageProcessingException( "Cannot draw image because the source image does not overlap the target image."); } InterpolateProcessor <TPixelBg, TPixelFg> .RowOperation operation = new InterpolateProcessor <TPixelBg, TPixelFg> .RowOperation( source, image, blender, configuration, num, width, y, targetX, InterpolationValue); ParallelRowIterator.IterateRows <InterpolateProcessor <TPixelBg, TPixelFg> .RowOperation>( configuration, rectangle2, in operation); }
public static byte[] ResizeImageTo(byte[] img, int w, int h, bool fromOutside) { SixLabors.ImageSharp.Image <Rgba32> bmp = null; try { using (System.IO.MemoryStream ms = new System.IO.MemoryStream(img)) bmp = SixLabors.ImageSharp.Image.Load(ms); using (var resultImg = ResizeImageTo(bmp, w, h, fromOutside)) { using (System.IO.MemoryStream outMS = new System.IO.MemoryStream()) { resultImg.Save(outMS, new SixLabors.ImageSharp.Formats.Png.PngEncoder()); return(outMS.ToArray()); } } } finally { if (bmp != null) { bmp.Dispose(); } } }
private void ResizeWithImageSharp(string filePath, string resizedFilePath, int size) { using (SixLabors.ImageSharp.Image <Rgba32> image = SixLabors.ImageSharp.Image.Load(filePath)) { image.Mutate(ctx => ctx.Resize(size, size)); image.Save(resizedFilePath); } }
public static SixLabors.ImageSharp.Image <Rgba32> ResizeImageTo(SixLabors.ImageSharp.Image <Rgba32> img, int w, int h, bool fromOutside) { float sW; float sH; if (fromOutside) { if (img.Width > img.Height) { float ratio = (float)h / (float)img.Height; sW = img.Width * ratio; sH = h; } else { float ratio = (float)w / (float)img.Width; sW = w; sH = img.Height * ratio; } } else { if (img.Width > img.Height) { float ratio = (float)w / (float)img.Width; sW = w; sH = img.Height * ratio; if (sH > h) { ratio = h / sH; sH = h; sW = sW * ratio; } } else { float ratio = (float)h / (float)img.Height; sW = img.Width * ratio; sH = h; if (sW > w) { ratio = w / sW; sH = sH * ratio; sW = w; } } } Rectangle r = new Rectangle((int)(w / 2.0 - sW / 2.0), (int)(h / 2.0 - sH / 2.0), (int)sW, (int)sH); Image <Rgba32> outImg = new Image <Rgba32>(w, h); outImg.Mutate(op => op.DrawImage(img, 1, new Size(r.Width, r.Height), new Point(r.X, r.Y))); return(outImg); }
private async Task renderResult(SixLabors.ImageSharp.Image <Rgba32> img) { stream = new MemoryStream(); img.SaveAsBmp(stream); stream.Position = 0; var bii = await BitmapFactory.FromStream(stream); imgResult.Source = bii; }
public static bool WriteTga(Bitmap imageToConvert, string outputPath) { bool success = false; SixLabors.ImageSharp.Image <Rgba32> img = Utility.ToImageSharpImage(imageToConvert); img.SaveAsTga(outputPath); success = true; return(success); }
public void MakeTexture(Device device, string strFilename) { if (!File.Exists(strFilename)) // #27122 2012.1.13 from: ImageInformation では FileNotFound 例外は返ってこないので、ここで自分でチェックする。わかりやすいログのために。 { throw new FileNotFoundException(string.Format("File does not exist. \n[{0}]", strFilename)); } using (SixLabors.ImageSharp.Image <Rgba32> image = SixLabors.ImageSharp.Image.Load <Rgba32>(strFilename)) MakeTexture(device, image, false); }
public static System.Drawing.Bitmap ToBitmap <TPixel>(this SixLabors.ImageSharp.Image <TPixel> image) where TPixel : unmanaged, IPixel <TPixel> { using (var memoryStream = new MemoryStream()) { var imageEncoder = image.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance); image.Save(memoryStream, imageEncoder); memoryStream.Seek(0, SeekOrigin.Begin); return(new System.Drawing.Bitmap(memoryStream)); } }
public static System.Drawing.Bitmap ToBitmap(SixLabors.ImageSharp.Image <Rgba32> image) { Stream stream = new System.IO.MemoryStream(); SixLabors.ImageSharp.Formats.Bmp.BmpEncoder bmpEncoder = new SixLabors.ImageSharp.Formats.Bmp.BmpEncoder(); // we need an encoder to preserve transparency. bmpEncoder.BitsPerPixel = SixLabors.ImageSharp.Formats.Bmp.BmpBitsPerPixel.Pixel32; // bitmap transparency needs 32 bits per pixel before we set transparency support. bmpEncoder.SupportTransparency = true; image.SaveAsBmp(stream, bmpEncoder); System.Drawing.Image img = System.Drawing.Image.FromStream(stream); return(new System.Drawing.Bitmap(stream)); }
public static bool WriteDds(Bitmap imageToConvert, BcEncoder bcEncoder, string outputPath) { bool success = false; FileStream fs = null; SixLabors.ImageSharp.Image <Rgba32> img = Utility.ToImageSharpImage(imageToConvert); fs = File.OpenWrite(outputPath); bcEncoder.Encode(img, fs); fs.DisposeAsync(); success = true; return(success); }
public byte[] GetVideoImageByte(int imageId, int?maxWidth) { var imageInfo = this._imageRepo.Get(imageId); if (imageInfo != null) { var imageRootPath = this._settingService.GetSettingValue(SettingKey.ImageDir); var imageFullPath = imageRootPath + imageInfo.Path.Replace("/", "\\"); var imageExt = Path.GetExtension(imageInfo.Path); if (System.IO.File.Exists(imageFullPath)) { // 原图输出 if (!maxWidth.HasValue) { return(System.IO.File.ReadAllBytes(imageFullPath)); } else { // 处理缩略图 using (SixLabors.ImageSharp.Image <Rgba32> srcImg = SixLabors.ImageSharp.Image.Load(imageFullPath)) { int resetWidth = maxWidth.Value; if (resetWidth > srcImg.Width) { resetWidth = srcImg.Width; } // 缩细倍数 var imgP = (Convert.ToDouble(resetWidth) / Convert.ToDouble(srcImg.Width)); var resetHeight = Convert.ToInt32(Convert.ToDouble(srcImg.Height) * imgP); srcImg.Mutate(x => x.Resize(resetWidth, resetHeight)); using (System.IO.MemoryStream ms = new MemoryStream()) { srcImg.Save(ms, SixLabors.ImageSharp.ImageFormats.Jpeg); byte[] imageByte = ms.ToArray(); return(imageByte); } } } } } return(null); }
/// <summary> /// Initializes a new instance of the <see cref="T:SixLabors.ImageSharp.Processing.Processors.Drawing.DrawImageProcessor`2" /> class. /// </summary> /// <param name="configuration">The configuration which allows altering default behaviour or extending the library.</param> /// <param name="image">The foreground <see cref="T:SixLabors.ImageSharp.Image`1" /> to blend with the currently processing image.</param> /// <param name="source">The source <see cref="T:SixLabors.ImageSharp.Image`1" /> for the current processor instance.</param> /// <param name="sourceRectangle">The source area to process for the current processor instance.</param> /// <param name="location">The location to draw the blended image.</param> /// <param name="colorBlendingMode">The blending mode to use when drawing the image.</param> /// <param name="alphaCompositionMode">The Alpha blending mode to use when drawing the image.</param> /// <param name="interpolationValue">The interpolation progress. Must be between 0 and 1.</param> public InterpolateProcessor(Configuration configuration, SixLabors.ImageSharp.Image <TPixelFg> image, SixLabors.ImageSharp.Image <TPixelBg> source, SixLabors.ImageSharp.Rectangle sourceRectangle, Point location, PixelColorBlendingMode colorBlendingMode, PixelAlphaCompositionMode alphaCompositionMode, float interpolationValue) : base(configuration, source, sourceRectangle) { // Guard.MustBeBetweenOrEqualTo(opacity, 0.0f, 1f, nameof(opacity)); this.Image = image; this.InterpolationValue = interpolationValue; this.Blender = PixelOperations <TPixelBg> .Instance.GetPixelBlender(colorBlendingMode, alphaCompositionMode); this.Location = location; }
public async Task OnKeyUpAsync() { idx++; idx %= colors.Length; using (var img = new SixLabors.ImageSharp.Image <Rgba32>(72, 72, colors[idx])) { await manager.SetImageAsync(img); } if (lastShowOk) { await manager.ShowOkAsync(); } else { await manager.ShowAlertAsync(); } lastShowOk = !lastShowOk; }
public RowOperation(ImageFrame <TPixelBg> sourceFrame, SixLabors.ImageSharp.Image <TPixelFg> targetImage, PixelBlender <TPixelBg> blender, Configuration configuration, int minX, int width, int locationY, int targetX, float interpolationValue) { this.sourceFrame = sourceFrame; this.targetImage = targetImage; this.blender = blender; this.configuration = configuration; this.minX = minX; this.width = width; this.locationY = locationY; this.targetX = targetX; this.interpolationValue = interpolationValue; }
/// <summary> /// 获取图片二进制数据 /// </summary> /// <param name="path">物理路径</param> /// <param name="maxWidth">最大宽度</param> /// <returns></returns> protected byte[] GetImageByte(string path, int?maxWidth) { if (System.IO.File.Exists(path)) { var imageFullPath = path.Replace("/", "\\"); var imageExt = Path.GetExtension(imageFullPath); // 原图输出 if (!maxWidth.HasValue) { return(System.IO.File.ReadAllBytes(imageFullPath)); } else { // 处理缩略图 using (SixLabors.ImageSharp.Image <Rgba32> srcImg = SixLabors.ImageSharp.Image.Load(imageFullPath)) { int resetWidth = maxWidth.Value; if (resetWidth > srcImg.Width) { resetWidth = srcImg.Width; } // 缩细倍数 var imgP = (Convert.ToDouble(resetWidth) / Convert.ToDouble(srcImg.Width)); var resetHeight = Convert.ToInt32(Convert.ToDouble(srcImg.Height) * imgP); srcImg.Mutate(x => x.Resize(resetWidth, resetHeight)); using (System.IO.MemoryStream ms = new MemoryStream()) { srcImg.Save(ms, SixLabors.ImageSharp.ImageFormats.Jpeg); byte[] imageByte = ms.ToArray(); return(imageByte); } } } } return(null); }
public SixLabors.ImageSharp.Image ToImage() { var image = new SixLabors.ImageSharp.Image <Rgba32>(WorldMapGenerateMap.SIZE, WorldMapGenerateMap.SIZE); for (int y = 0; y < WorldMapGenerateMap.SIZE; y++) { Span <Rgba32> pixelRowSpan = image.GetPixelRowSpan(y); for (int x = 0; x < WorldMapGenerateMap.SIZE; x++) { if (colorMap.ContainsKey(_worldMapTiles[x, y])) { pixelRowSpan[x] = colorMap[_worldMapTiles[x, y]]; } else { pixelRowSpan[x] = SixLabors.ImageSharp.Color.White; } } } return(image); }
private void button_genPrev_Click(object sender, EventArgs e) { SixLabors.ImageSharp.Image <Bgra32> tmpImg = ImageSharpExtensions.ToImageSharpImage(_img); var pf = (PvrPixelFormat)comboBox_PixelFormat.SelectedItem; var df = (PvrDataFormat)comboBox_DataFormat.SelectedItem; var dr = comboBox_Dithering.SelectedIndex; var em = checkBox_eyeMode.Checked; using (var ms = new MemoryStream()) { // Temp encode to pvr PvrTextureEncoder tmpImgRaw = new PvrTextureEncoder(tmpImg, pf, df); tmpImgRaw.DitheringMode = dr; if (em == false) { tmpImgRaw.MetricMode = 0; } else { tmpImgRaw.MetricMode = 1; } tmpImgRaw.Save(ms); ms.Seek(0, SeekOrigin.Begin); //Tmp decode for preview var tmpPreview = new PvrTextureDecoder(ms); var img = tmpPreview.GetImage(); var imgNative = ImageSharpExtensions.ToBitmap(img); pictureBox_PVRPreview.Image = imgNative; pictureBox_PVRPreview.Update(); panel1.Update(); } }
public void MakeTexture(Device device, SixLabors.ImageSharp.Image <Rgba32> bitmap, bool b黒を透過する) { bitmap.Mutate(c => c.Flip(FlipMode.Vertical)); if (b黒を透過する) { bitmap.Mutate(c => c.BackgroundColor(SixLabors.ImageSharp.Color.Transparent)); } try { this.szTextureSize = new Size(bitmap.Width, bitmap.Height); this.rcImageRect = new Rectangle(0, 0, this.szTextureSize.Width, this.szTextureSize.Height); //VBOをここで生成する this.vrtVBO = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, (int)this.vrtVBO); GL.BufferData(BufferTarget.ArrayBuffer, this.vertices.Length * Vector3.SizeInBytes, vertices, BufferUsageHint.DynamicDraw); this.texVBO = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, (int)this.texVBO); GL.BufferData(BufferTarget.ArrayBuffer, this.texcoord.Length * Vector2.SizeInBytes, texcoord, BufferUsageHint.DynamicDraw); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); this.texture = GL.GenTexture(); //テクスチャ用バッファのひもづけ GL.BindTexture(TextureTarget.Texture2D, (int)this.texture); //テクスチャの設定 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, 3); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinLod, 0); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLod, 3); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.LinearSharpenSgis); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); if (bitmap.TryGetSinglePixelSpan(out Span <Rgba32> span)) { GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bitmap.Width, bitmap.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, span.ToArray()); } else { throw new CTextureCreateFailedException("GetPixelData failed"); } GL.Hint(HintTarget.GenerateMipmapHint, HintMode.Nicest); GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); UpdateTexCoordArray(0, 0, 0, 1, 1, 1, 1, 0); //TextureCoordinateの初期化 GL.BindTexture(TextureTarget.Texture2D, 0); this.bTextureDisposed = false; } catch { this.Dispose(); throw new CTextureCreateFailedException(string.Format("Failed to create texture. \n")); } }
public ImageSharpImage(SixLabors.ImageSharp.Image <T> image) { _image = image; }