I'm trying to make some gradient & preview it in a pictureBox without saving but it seems not working:
if (isChanged == true)
{
re = new Rectangle(0, 0, int.Parse(textBox5.Text), int.Parse(textBox4.Text));
currectBrush = new System.Drawing.Drawing2D.LinearGradientBrush(re, System.Drawing.ColorTranslator.FromHtml("#FC00FF"), System.Drawing.ColorTranslator.FromHtml("#00DBDE"), -45f); ;
bitmap = new Bitmap(int.Parse(textBox5.Text), int.Parse(textBox4.Text));
using (bitmap)
using (var graphics = Graphics.FromImage(bitmap))
{
currectBrush = new System.Drawing.Drawing2D.LinearGradientBrush(re, System.Drawing.ColorTranslator.FromHtml(textBox1.Text), System.Drawing.ColorTranslator.FromHtml(textBox2.Text), int.Parse(textBox3.Text));
graphics.FillRectangle(currectBrush, re);
pictureBox1.Image = bitmap;
}
isChanged = false;
}
And this is what pictureBox looks like after running this part of code:
Remove this line because it disposes the resources
using (bitmap)
Related
I want to load an image file .tif (that image has many classes of colors to indicate the height) into my picture box, when I use load image 2D as normal, it is displayed as a black image.
private static Bitmap ResizeImage(Bitmap image, Size newSize)
{
Bitmap newImage = new Bitmap(newSize.Width, newSize.Height);
using (Graphics GFX = Graphics.FromImage((Bitmap)newImage))
{
GFX.DrawImage(image, new Rectangle(Point.Empty, newSize));
}
return newImage;
}
private void loadImage_Click(object sender, EventArgs e)
{
OpenFileDialog opf = new OpenFileDialog();
opf.Title = "Select Image";
opf.Filter = "Image File (*.jpg; *.TIF; *.jpeg)| *.jpg; *.TIF; *.jpeg ";
if (opf.ShowDialog()==DialogResult.OK)
{
Bitmap image = new Bitmap(opf.FileName);
Image Image = ResizeImage(image, mapViewer.Size);
mapViewer.Image = Image;
Origin = Image;
width = Origin.Width;
height = Origin.Height;
}
}
I have a panel like this with many controls.I want to record this panel when a event fire and end when another event fire. I lost 2 days to find solution. I hope someone can help me deal it. tks
enter image description here
Is there anything you have tried?
How about this:
Add references:
PresentationCore.dll
System.Xaml.dll
WindowsBase.dll
add variable to your form:
List<Bitmap> bitmapList = new List<Bitmap>();
Call this metod to save panel's state (gif frame)
private void SavePanel()
{
Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, panel1.Width, panel1.Height));
bitmapList.Add(bmp);
}
Call this metod to create Gif from saved frames:
private void SaveGif()
{
System.Windows.Media.Imaging.GifBitmapEncoder gEnc = new System.Windows.Media.Imaging.GifBitmapEncoder();
foreach (var bmp in bitmapList)
{
var hbmp = bmp.GetHbitmap();
var src = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hbmp, IntPtr.Zero, System.Windows.Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
gEnc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(src));
}
using (FileStream fs = new FileStream(#"C:\panel1.gif", FileMode.Create))
{
gEnc.Save(fs);
}
bitmapList.Clear();
}
I use InkCanvas to implement signature function.
after signed, I can use RenderTargetBitmap class to save the signature-drawing into bitmap.
but RenderTargetBitmap always save InkCanvas itself, means can not save ONLY the signature content.
my question is, how to save StrokeCollection into bitmap?
I think you should use Win2D (Win2D.uwp NuGet package); it is pretty easy.
Here is the code:
async void SaveAsBitmap(object sender, RoutedEventArgs e)
{
//copy from origianl canvas and paste on the new canvas for saving
var strokes = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();
//check if canvas is not empty
if (strokes.Count == 0) return;
//select all the strokes to be copied to the clipboard
foreach (var stroke in strokes)
{
stroke.Selected = true;
}
inkCanvas.InkPresenter.StrokeContainer.CopySelectedToClipboard();
//paste the strokes to a new InkCanvas and move the strokes to the topper left corner
var newCanvas = new InkCanvas();
newCanvas.InkPresenter.StrokeContainer.PasteFromClipboard(new Point(0, 0));
//using Win2D to save ink as png
CanvasDevice device = CanvasDevice.GetSharedDevice();
CanvasRenderTarget renderTarget = new CanvasRenderTarget(device,
(int)inkCanvas.InkPresenter.StrokeContainer.BoundingRect.Width,
(int)inkCanvas.InkPresenter.StrokeContainer.BoundingRect.Height,
96);
using (var ds = renderTarget.CreateDrawingSession())
{
//ds.Clear(Colors.White); //uncomment this line for a white background
ds.DrawInk(newCanvas.InkPresenter.StrokeContainer.GetStrokes());
}
//save file dialog
var savePicker = new Windows.Storage.Pickers.FileSavePicker()
{
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary
};
savePicker.FileTypeChoices.Add("Image file", new List<string>() { ".jpeg", ".png" });
savePicker.SuggestedFileName = "mysign.png";
var file = await savePicker.PickSaveFileAsync();
if (file != null)
{
using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
await renderTarget.SaveAsync(fileStream, CanvasBitmapFileFormat.Png);
}
}
}
I am picking image from gallery using PhotoChooserTask, here is my setup
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
PhotoChooserTask photo = new PhotoChooserTask();
photo.Completed += photo_Completed;
photo.Show();
}
void photo_Completed(object sender, PhotoResult e)
{
if (e.ChosenPhoto != null)
{
WriteableBitmap wbmp1 = PictureDecoder.DecodeJpeg(e.ChoosenPhoto, (int)scrnWidth, (int)scrnHeight);
ImageBrush iBru = new ImageBrush();
iBru.ImageSource = wbmp1;
iBru.Stretch = Stretch.Fill;
ContentPanel.Background = iBru;
}
}
Problem: with this way is it works only with .JPEG images
to make it work with other image formats, I tried this:
void photo_Completed(object sender, PhotoResult e)
{
if (e.ChosenPhoto != null)
{
WriteableBitmap wBmp = new WriteableBitmap(0, 0);//6930432
wBmp.SetSource(e.ChosenPhoto);//23105536
MemoryStream tmpStream = new MemoryStream();//23105536
wBmp.SaveJpeg(tmpStream, (int)scrnWidth, (int)scrnHeight, 0, 100);//22831104
tmpStream.Seek(0, SeekOrigin.Begin);//22831104
WriteableBitmap wbmp1 = PictureDecoder.DecodeJpeg(tmpStream, (int)scrnWidth, (int)scrnHeight);//24449024
ImageBrush iBru = new ImageBrush();//24449024
iBru.ImageSource = wbmp1;//24449024
iBru.Stretch = Stretch.Fill;
ContentPanel.Background = iBru;
}
}
this way works with different image formats, but it is not memory efficient.
I have mentioned number of bytes used after each line for better understanding.
Question: In latter code snippet, I do not need wbmp anymore, How can I clear memory used by wbmp object?
As #Soonts suggested I used BitmapImage and it solves my purpose,
BitmapImage bmp = new BitmapImage();
bmp.DecodePixelWidth = (int)scrnWidth;
bmp.DecodePixelHeight = (int)scrnHeight;
bmp.SetSource(e.ChosenPhoto);
It consumes less memory and we can scale down image
Get rid of the WriteableBitmap, instead do something like this:
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource( e.ChosenPhoto );
var ib = new ImageBrush() { ImageSource = bmp, Stretch = Stretch.Fill };
ContentPanel.Background = ib;
I want to take a screenshot of my control on Winform. I use this function - find in this site :
public void GetImage()
{
var bm = new Bitmap(display1.Width, display1.Height);
display1.DrawToBitmap(bm, display1.ClientRectangle);
bm.Save(#"c:\whatever.gif", ImageFormat.Gif);
}
But when I'm using it, i have a error about 'System.Runtime.InteropServices.ExternalException' in System.Drawing.dll : A generic error occurred in GDI +.
Do you have any idea aboout that ?
Thanks !
----------------------EDIT---------------------------
Ok I have change my function like this :
public void GetImage()
{
var bm = new Bitmap(display1.Width, display1.Height);
display1.DrawToBitmap(bm, display1.ClientRectangle);
var dlg = new SaveFileDialog { DefaultExt = "png", Filter = "Png Files|*.png" };
var res = dlg.ShowDialog();
if (res == DialogResult.OK) bm.Save(dlg.FileName, ImageFormat.Png);
}
and it's works but I have now an empty picture :/
Try the following:
assuming display1 is a Rectangle that bounds the area of the display you wish to capture.
public void GetImage()
{
Rectangle display1 = this.Bounds; // winforms control bounds
// for full screen use "= Screen.GetBounds(Point.Empty);
var bm = new Bitmap(display1.Width, display1.Height);
//display1.DrawToBitmap(bm, display1.ClientRectangle);
Graphics g = Graphics.FromImage(bm);
g.CopyFromScreen(new Point(display1.Left, display1.Top), Point.Empty, display1.Size);
var dlg = new SaveFileDialog { DefaultExt = "png", Filter = "Png Files|*.png" };
var res = dlg.ShowDialog();
if (res == DialogResult.OK) bm.Save(dlg.FileName, ImageFormat.Png);
}