public Stream Resize(int width, int height)
 {
     System.IO.MemoryStream ms = new MemoryStream();
     _image.Mutate(x => x.Resize(width, height));
     _image.SaveAsPng(ms);
     return(ms);
 }
示例#2
0
 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);
     }
 }
示例#3
0
        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>
        /// 获取图片二进制数据
        /// </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);
        }
示例#5
0
        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"));
            }
        }