Ejemplo n.º 1
0
 public override void SaveImage(MemBitmap bitmap, string filename, OutputImageFormat outputFormat, object saveParameters)
 {
     //using (FileStream fs = new FileStream(filename, FileMode.Create))
     //{
     //    SaveImage(bitmap, fs, outputFormat, saveParameters);
     //}
 }
Ejemplo n.º 2
0
        /// <summary>
        /// <inheritdoc />
        /// </summary>
        public StoragePathModel PrepareStoragePath(OutputImageFormat format)
        {
            var uploadKeyGuid = Guid.NewGuid();
            var uploadKey     = uploadKeyGuid.ToString("N");

            var basePath = this.appSettings.Value.Directory.WorkingDirectory;

            var segmentsPath = Path.Combine(
                uploadKey[0].ToString(),
                uploadKey[1].ToString(),
                uploadKey[2].ToString());

            var baseDirectoryPath = Path.Combine(basePath, segmentsPath);
            var finalPath         = $"{Path.Combine(baseDirectoryPath, uploadKey)}.{format.GetImageFormat()}";

            var urlPath = $"/{segmentsPath}/{uploadKey}.{format.GetImageFormat()}";

            // If storage directory doesn't exists, create it
            if (!Directory.Exists(baseDirectoryPath))
            {
                Directory.CreateDirectory(baseDirectoryPath);
            }

            return(new StoragePathModel
            {
                Key = uploadKeyGuid,
                FilePath = finalPath,
                UrlPath = urlPath
            });
        }
Ejemplo n.º 3
0
        public MemBitmap LoadImage(Stream input, OutputImageFormat format)
        {
            ImageTools.ExtendedImage extendedImg = new ImageTools.ExtendedImage();
            //TODO: review img loading, we should not use only its extension
            switch (format)
            {
            case OutputImageFormat.Jpeg:
            {
                var decoder = new ImageTools.IO.Jpeg.JpegDecoder();
                var dst     = new JpegDecoderDst();
                extendedImg.JpegDecompressDest = dst;
                extendedImg.Load(input, decoder);
                //copy from

                return(dst.MemBitmapOutput);
            }
            break;

            case OutputImageFormat.Png:
            {
                var decoder = new ImageTools.IO.Png.PngDecoder();
                extendedImg.Load(input, decoder);
            }
            break;

            default:
                throw new System.NotSupportedException();
            }

            //assume 32 bit ??
            byte[] pixels = extendedImg.Pixels;
            unsafe
            {
                fixed(byte *p_src = &pixels[0])
                {
                    PixelFarm.CpuBlit.MemBitmap memBmp = PixelFarm.CpuBlit.MemBitmap.CreateFromCopy(
                        extendedImg.PixelWidth,
                        extendedImg.PixelHeight,
                        (IntPtr)p_src,
                        pixels.Length,
                        false
                        );

                    memBmp.IsBigEndian = true;
                    return(memBmp);
                }
            }

            ////PixelFarm.CpuBlit.MemBitmap memBmp = PixelFarm.CpuBlit.MemBitmap.CreateFromCopy(
            ////    extendedImg.PixelWidth,
            ////    extendedImg.PixelHeight,
            ////    extendedImg.PixelWidth * 4, //assume
            ////    32, //assume?
            ////    extendedImg.Pixels,
            ////    false
            ////    );
            ////the imgtools load data as BigEndian
            //memBmp.IsBigEndian = true;
            //return memBmp;
        }
Ejemplo n.º 4
0
 public override void SaveImage(MemBitmap bitmap, string filename, OutputImageFormat outputFormat, object saveParameters)
 {
     //using (FileStream fs = new FileStream(LocalFileStorageProvider.s_globalBaseDir + "/" + filename, FileMode.Create))
     //{
     //    SaveImage(bitmap, fs, outputFormat, saveParameters);
     //}
 }
Ejemplo n.º 5
0
        public override void SaveImage(MemBitmap bitmap, Stream output, OutputImageFormat outputFormat, object saveParameters)
        {
            //save img to
            using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                                           System.Drawing.Imaging.ImageLockMode.WriteOnly,
                                           System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                unsafe
                {
                    byte *ptr = (byte *)MemBitmap.GetBufferPtr(bitmap).Ptr;
                    MemMx.memcpy((byte *)bmpdata.Scan0, ptr, bmpdata.Stride * bmp.Height);
                }
                bmp.UnlockBits(bmpdata);
                //save to stream
                System.Drawing.Imaging.ImageFormat format = null;
                switch (outputFormat)
                {
                case OutputImageFormat.Default:
                    throw new NotSupportedException();

                case OutputImageFormat.Jpeg:
                    format = System.Drawing.Imaging.ImageFormat.Jpeg;
                    break;

                case OutputImageFormat.Png:
                    format = System.Drawing.Imaging.ImageFormat.Png;
                    break;
                }
                bmp.Save(output, format);
            }
        }
Ejemplo n.º 6
0
 public static string GetMimeType(this OutputImageFormat val)
 {
     return(val.GetType()
            .GetMember(val.ToString())
            .FirstOrDefault()
            ?.GetCustomAttribute <MimeTypeAttribute>(false)
            ?.Description
            ?? val.ToString());
 }
Ejemplo n.º 7
0
        public async Task <IActionResult> ToJpeg(OutputImageFormat type)
        {
            var file = this.GetDefaultFile();

            if (file == null)
            {
                return(BadRequest());
            }

            return(await File(file, type));
        }
Ejemplo n.º 8
0
        public override void SaveImage(MemBitmap bitmap, Stream output, OutputImageFormat outputFormat, object saveParameters)
        {
            switch (outputFormat)
            {
            case OutputImageFormat.Png:
                PngIOStorage.Save(bitmap, output);
                break;

            default:
                throw new NotSupportedException();
            }

            //throw new NotImplementedException();
        }
Ejemplo n.º 9
0
        private static MagickFormat ToMagickFormat(OutputImageFormat outputFormat)
        {
            var magickFormat = outputFormat switch
            {
                OutputImageFormat.Gif => MagickFormat.Gif,
                OutputImageFormat.Png => MagickFormat.Png,
                _ => MagickFormat.Jpeg
            };

            return(magickFormat);
        }

        #endregion
    }
Ejemplo n.º 10
0
        private async Task <IActionResult> File(IFormFile file, OutputImageFormat format)
        {
            var outputStream = new MemoryStream();

            await using var imageStream = file.OpenReadStream();

            await this.imageConvertingService.ImageToFormat(imageStream, new StreamImageSavingParamsModel
            {
                Format       = OutputImageFormat.Jpeg,
                Quality      = Constants.DefaultJpegQuality,
                OutputStream = outputStream
            });

            return(File(outputStream, format.GetMimeType()));
        }
Ejemplo n.º 11
0
        public MemBitmap LoadImage(Stream input, OutputImageFormat format)
        {
            ImageTools.ExtendedImage extendedImg = new ImageTools.ExtendedImage();
            //TODO: review img loading, we should not use only its extension
            switch (format)
            {
            case OutputImageFormat.Png:
            {
                var decoder = new ImageTools.IO.Png.PngDecoder();
                extendedImg.Load(input, decoder);
            }
            break;

            case OutputImageFormat.Jpeg:
            {
                var decoder = new ImageTools.IO.Jpeg.JpegDecoder();
                extendedImg.Load(input, decoder);
            }
            break;

            default:
                throw new System.NotSupportedException();
            }
            //var decoder = new ImageTools.IO.Png.PngDecoder();


            //assume 32 bit

            PixelFarm.CpuBlit.MemBitmap memBmp = PixelFarm.CpuBlit.MemBitmap.CreateFromCopy(
                extendedImg.PixelWidth,
                extendedImg.PixelHeight,
                extendedImg.Pixels32
                );
            //the imgtools load data as BigEndian
            memBmp.IsBigEndian = true;
            return(memBmp);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Converts to file with given name and format
 /// </summary>
 /// <param name="filename">The output file name to save the image to</param>
 /// <param name="imageFormat">The output image file format. Example: JPEG, GIF, PNG</param>
 /// <returns>True on success, false on error</returns>
 public bool toFile(string filename, OutputImageFormat imageFormat)
 {
     this.output = new Output(OutputType.File, imageFormat, filename);
     return(this.convertToFile());
 }
Ejemplo n.º 13
0
 public Output(OutputType type, OutputImageFormat imageFormat)
     : this(type, imageFormat, null)
 {
 }
Ejemplo n.º 14
0
 public Output(OutputType type, OutputImageFormat imageFormat, string value)
 {
     this.type        = type;
     this.imageFormat = imageFormat;
     this.value       = value;
 }
 /// <summary>
 /// Converts to file with given name and format
 /// </summary>
 /// <param name="filename">The output file name to save the image to</param>
 /// <param name="imageFormat">The output image file format. Example: JPEG, GIF, PNG</param>
 /// <returns>True on success, false on error</returns>
 public bool toFile(string filename, OutputImageFormat imageFormat)
 {
     this.output = new Output(OutputType.File, imageFormat, filename);
     return this.convertToFile();
 }
 /// <summary>
 /// Converts to base64 encoded string with given output image format
 /// </summary>
 /// <param name="imageFormat">The desired output image format. Example: JPEG, GIF, PNG</param>
 /// <returns>The base64 encoded string that represents the image</returns>
 public string toBase46EncodedString(OutputImageFormat imageFormat)
 {
     this.output = new Output(OutputType.Base64EncodedString, imageFormat);
     return this.convertToBase46EncodedString();
 }
Ejemplo n.º 17
0
 public Output(OutputType type, OutputImageFormat imageFormat)
     : this(type, imageFormat, null)
 {
 }
Ejemplo n.º 18
0
 public abstract void SaveImage(MemBitmap bitmap, string filename, OutputImageFormat outputFormat, object saveParameters);
Ejemplo n.º 19
0
 public abstract void SaveImage(MemBitmap bitmap, System.IO.Stream output, OutputImageFormat outputFormat, object saveParameters);
Ejemplo n.º 20
0
        /// <summary>
        /// <inheritdoc />
        /// </summary>
        public ImageThumbPathsModel PrepareThumbFilePath(StoragePathModel originalPath, OutputImageFormat format, int width, int height)
        {
            // Prepare a disk path
            var diskPath = $"{Path.ChangeExtension(originalPath.FilePath, null)}_thumb_{width}x{height}.{format.GetImageFormat()}";

            // Prepare an URL path
            var url = this.NormalizeWebPath(Path.ChangeExtension(originalPath.UrlPath, null)) +
                      $"_thumb_{width}x{height}.{format.GetImageFormat()}";

            return(new ImageThumbPathsModel
            {
                DiskPath = diskPath,
                Url = url
            });
        }
Ejemplo n.º 21
0
 public Output(OutputType type, OutputImageFormat imageFormat, string value)
 {
     this.type = type;
     this.imageFormat = imageFormat;
     this.value = value;
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Converts to base64 encoded string with given output image format
 /// </summary>
 /// <param name="imageFormat">The desired output image format. Example: JPEG, GIF, PNG</param>
 /// <returns>The base64 encoded string that represents the image</returns>
 public string toBase46EncodedString(OutputImageFormat imageFormat)
 {
     this.output = new Output(OutputType.Base64EncodedString, imageFormat);
     return(this.convertToBase46EncodedString());
 }
Ejemplo n.º 23
0
 public override void SaveImage(MemBitmap bitmap, string filename, OutputImageFormat outputFormat, object saveParameters)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 24
0
        public async Task StartFrameReaderAsync(InputSource inputSource, OutputImageFormat outputImageFormat, CancellationToken cancellationToken)
        {
            var inputArgs  = $"-y {inputSource.InputCommand}";
            var outputArgs = $"-c:v {outputImageFormat.ToString().ToLower()} -f image2pipe -";

            var startInfo = new ProcessStartInfo
            {
                FileName               = this._ffmpegPath,
                Arguments              = $"{inputArgs} {outputArgs}",
                UseShellExecute        = false,
                CreateNoWindow         = true,
                RedirectStandardInput  = true,
                RedirectStandardError  = true,
                RedirectStandardOutput = true,
            };

            using (var ffmpegProcess = new Process {
                StartInfo = startInfo
            })
            {
                ffmpegProcess.ErrorDataReceived += this.ProcessDataReceived;

                ffmpegProcess.Start();
                ffmpegProcess.BeginErrorReadLine();

                using (var frameOutputStream = ffmpegProcess.StandardOutput.BaseStream)
                {
                    var    index       = 0;
                    var    buffer      = new byte[32768];
                    var    imageData   = new List <byte>();
                    byte[] imageHeader = null;

                    while (!cancellationToken.IsCancellationRequested)
                    {
                        var length = await frameOutputStream.ReadAsync(buffer, 0, buffer.Length);

                        if (length == 0)
                        {
                            break;
                        }

                        if (imageHeader == null)
                        {
                            imageHeader = buffer.Take(5).ToArray();
                        }

                        if (buffer.Take(5).SequenceEqual(imageHeader))
                        {
                            if (imageData.Count > 0)
                            {
                                this.NewImageReceived?.Invoke(imageData.ToArray());
                                imageData.Clear();
                                index++;
                            }
                        }

                        imageData.AddRange(buffer.Take(length));
                    }

                    frameOutputStream.Close();
                }

                ffmpegProcess.ErrorDataReceived -= this.ProcessDataReceived;

                ffmpegProcess.WaitForExit(1000);

                if (!ffmpegProcess.HasExited)
                {
                    ffmpegProcess.Kill();
                }
            }
        }