Example #1
0
        async static public Task <int> SaveOrUpload(this IBitmap bitmap, string path, ImageFormat format)
        {
            if (path.StartsWith("http://") || path.StartsWith("https://"))
            {
                string url      = path.Substring(0, path.LastIndexOf("/"));
                string filename = path.Substring(path.LastIndexOf("/") + 1);

                Console.WriteLine($"upload {filename} to {url}");

                MemoryStream ms = new MemoryStream();
                bitmap.Save(ms, format);

                var file_bytes = ms.ToArray();

                // reuse HttpClient
                var client = _httpClient ?? (_httpClient = new HttpClient());

                try
                {
                    using (var requestMessage = new HttpRequestMessage())
                    {
                        MultipartFormDataContent form = new MultipartFormDataContent();
                        form.Add(new ByteArrayContent(file_bytes, 0, file_bytes.Length), "file", filename);

                        requestMessage.Method  = HttpMethod.Post;
                        requestMessage.Content = form;
                        requestMessage.SetRequestUrl(url);

                        HttpResponseMessage response = await client.SendAsync(requestMessage);

                        if (!response.IsSuccessStatusCode)
                        {
                            throw new Exception($"SaveOrUpload: Upload status code: { response.StatusCode }");
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception($"SaveOrUpload: { ex.Message }", ex);
                }

                return(file_bytes.Length);
            }
            else
            {
                bitmap.Save(path, format);
                return(1);
            }
        }
Example #2
0
        static public void SaveBitmap(IBitmap bitmap, string filename)
        {
            bitmap.Save(filename, filename.EndsWith(".jpg") ? ImageFormat.Jpeg : ImageFormat.Png);

            //BitmapPixelData bmPixelData = null;
            //try
            //{
            //    bmPixelData = bitmap.Clone(PixelFormat.Format32bppArgb).LockBitmapPixelData(BitmapLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            //    using (var bm = new System.Drawing.Bitmap(bmPixelData.Width,
            //                      bmPixelData.Height,
            //                      bmPixelData.Stride,
            //                      System.Drawing.Imaging.PixelFormat.Format32bppArgb,
            //                      bmPixelData.Scan0))
            //    {
            //        bm.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
            //    }
            //}
            //finally
            //{
            //    if (bmPixelData != null)
            //    {
            //        bitmap.UnlockBitmapPixelData(bmPixelData);
            //    }
            //}
        }
Example #3
0
        /// <summary>
        /// Saves the bitmap as a PNG file having generated a name for it from the specified name and the dimensions.
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="name">The base name for the file.</param>
        private void SaveBitmap(IBitmap bitmap, string name)
        {
            if (!folderForNewRegressionTestOutput.Exists)
            {
                folderForNewRegressionTestOutput.Create();
            }
            name = GetFileName(name, bitmap.Width, bitmap.Height);

            bitmap.Save(Path.Combine(folderForNewRegressionTestOutput.FullName, name), ImageFormat.Png);
        }
Example #4
0
        private async Task SaveImageToBlobCacheAsync(string key, IBitmap bitmap)
        {
            using (var ms = new MemoryStream())
            {
                try
                {
                    await bitmap.Save(CompressedBitmapFormat.Jpeg, 1, ms);
                }

                catch (NotSupportedException ex)
                {
                    throw new ArtworkCacheException("Couldn't save artwork", ex);
                }

                await this.cache.Insert(key, ms.ToArray());
            }
        }
        /// <summary>
        /// Saves an Bitmap image to disk
        /// </summary>
        /// <param name="pic">The Image</param>
        /// <param name="folderPath">Save Location</param>
        /// <param name="fileName">Save Filename</param>
        private async void SavePicture(IBitmap pic, string folderPath, string fileName)
        {
            //if null default the users Download folder
            string basePath = string.IsNullOrEmpty(folderPath) ? System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads") : folderPath;
            string filePath = System.IO.Path.Combine(basePath, $"{fileName}.png");

            //save the image
            try
            {
                using (var file = System.IO.File.Create(filePath))
                {
                    pic.Save(file);
                }
            }
            catch (Exception e)
            {
                //TODO: Log error
                await MessageBox.Show(MainWindow.Instance, e.Message, "ERROR", MessageBox.MessageBoxButtons.Ok);
            }
        }
Example #6
0
        private unsafe void UpdateIcon(IBitmap icon)
        {
            if (icon is null)
            {
                SetIcon(null, IntPtr.Zero);
            }
            else
            {
                using (var ms = new MemoryStream())
                {
                    icon.Save(ms);

                    var imageData = ms.ToArray();

                    fixed(void *ptr = imageData)
                    {
                        SetIcon(ptr, new IntPtr(imageData.Length));
                    }
                }
            }
        }
Example #7
0
        private async Task SaveImageToBlobCacheAsync(string key, IBitmap bitmap)
        {
            using (var ms = new MemoryStream())
            {
                try
                {
                    await bitmap.Save(CompressedBitmapFormat.Jpeg, 1, ms);
                }

                catch (NotSupportedException ex)
                {
                    throw new ArtworkCacheException("Couldn't save artwork", ex);
                }

                await this.cache.Insert(key, ms.ToArray());
            }
        }
Example #8
0
 /// <summary>
 /// Saves the bitmap to a stream in the specified format
 /// </summary>
 /// <param name="stream">Stream to save the bitmap to</param>
 /// <param name="format">Format to save as</param>
 public void Save(Stream stream, ImageFormat format)
 {
     handler.Save(stream, format);
 }