I've got a normal picturebox in a form, I have an image of a signature that I recover from the database, when I save the image with the signature it had a white background and the signature was black, but when I recover the image it has a black background and the signature is white.
Can anyone explain why this happens and how can I fix it??
You can find the image at the following direction
http://www.mediafire.com/?c91awvyrya98m2c
It is contained in a rar file: Signature.jpg
The code is the following:
using System.Drawing;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
namespace WindowsFormsApplication3
{
class TestForm : System.Windows.Forms.Form
{
PictureBox oPictureBoxSignature;
public Database Db;
public TestForm()
{
Db = DatabaseFactory.CreateDatabase("DBTest");
oPictureBoxSignature = new PictureBox();
}
public bool Save_Record()
{
byte[] Signature = imageToByteArray(oPictureBoxSignature.Image);
return Save(Signature);
}
public bool Save(byte[] Signature)
{
using (DbCommand dbCmd = Db.GetStoredProcCommand("Save_Signature"))
{
Db.AddInParameter(dbCmd, "Signature", DbType.Binary, Signature);
return Db.ExecuteNonQuery(dbCmd) > 0;
}
}
public void Recover_Record(byte[] Signature)
{
oPictureBoxSignature.Image = byteArrayToImage(Signature);
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
if (byteArrayIn != null)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
else
return null;
}
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
if (imageIn != null)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
else
return null;
}
}
}
Related
I'm trying to create an SVG file from some shape objects(path geometries, ellipses etc.) drawn on XAML canvas controls (the canvases are rendered on top of each other inside grid controls). It looks like Win2D can provide the classes to generate the SVG file, but I'm struggling to figure out how to populate the CanvasSvgDocument class with the shapes.
This is the only partial example I've found, but the answer seems to include a conversion to XML strings to load into the CanvasSvgDocument which seems like doing the same task twice (as SVG files are XML). Is anybody able to provide an example of how I might do this?
My current best guess of what the resulting code might look like is:
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Svg;
using Microsoft.Graphics.Canvas.UI.Xaml;
using System;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
namespace MyApp
{
public class ExportSVG
{
private CanvasSvgDocument SVG { get; } = new(new CanvasDevice());
public async Task SaveASync(IRandomAccessStream stream) => await SVG.SaveAsync(stream);
public void AddCanvases(UIElement element)
{
if (element is Grid grid)
{
foreach (UIElement child in grid.Children)
{
AddCanvases(child);
}
}
else if (element is Canvas canvas)
{
AddCanvas(canvas);
}
}
public void AddCanvas(Canvas canvas)
{
foreach (UIElement element in canvas.Children)
{
if (element is Path path)
{
if (path.Data is PathGeometry pathGeometry)
{
foreach (PathFigure pathFigure in pathGeometry.Figures)
{
// Add path to SVG
}
}
else if (path.Data is EllipseGeometry ellipseGeometry)
{
// Add ellipse to SVG
}
}
else if (element is TextBlock textBlock)
{
// add text to SVG
}
}
}
}
}
You could use CanvasGeometry.CreateInk convert the ink strokes into geometry and use the relevant methods under the CanvasGeometry namespace to get the path, then write a custom class to read the parse path. Finally, the generated CanvasSvgDocument object is used to save the stream containing the svg content.
Please refer to the following sample to do these steps. (Note: download Win2D.uwp package)
XAML code:
<Page
x:Class="CanvasToSVG.MainPage"
…
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<StackPanel>
<InkCanvas x:Name="MyInkConntrol" Height="500">
</InkCanvas>
<InkToolbar Grid.Row="1" TargetInkCanvas="{x:Bind MyInkConntrol}" HorizontalAlignment="Left">
<InkToolbarCustomToolButton Click="save">
<SymbolIcon Symbol="Save" />
</InkToolbarCustomToolButton>
</InkToolbar>
<Line Stroke="Black"/>
<Image Name="ImageControl"></Image>
</StackPanel>
</Grid>
</Page>
Code behind:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
MyInkConntrol.InkPresenter.InputDeviceTypes= CoreInputDeviceTypes.Mouse |CoreInputDeviceTypes.Pen |
CoreInputDeviceTypes.Touch;
MyInkConntrol.InkPresenter.StrokesCollected += InkPresenter_StrokesCollected;
MyInkConntrol.InkPresenter.StrokesErased += InkPresenter_StrokesErased;
}
private async void InkPresenter_StrokesErased(Windows.UI.Input.Inking.InkPresenter sender, Windows.UI.Input.Inking.InkStrokesErasedEventArgs args)
{
await RenderSvg();
}
private async void InkPresenter_StrokesCollected(Windows.UI.Input.Inking.InkPresenter sender, Windows.UI.Input.Inking.InkStrokesCollectedEventArgs args)
{
await RenderSvg();
}
public async Task RenderSvg()
{
using (var stream=new InMemoryRandomAccessStream())
{
await RenderSvg(stream);
var image= new SvgImageSource();
await image.SetSourceAsync(stream);
ImageControl.Source = image;
}
}
public async Task RenderSvg(IRandomAccessStream randomAccessStream)
{
var sharedDevice = CanvasDevice.GetSharedDevice();
using (var offscreen = new CanvasRenderTarget(sharedDevice, (float)MyInkConntrol.RenderSize.Width, (float)MyInkConntrol.RenderSize.Height, 96))
{
using (var session = offscreen.CreateDrawingSession())
{
var svgDocument = new CanvasSvgDocument(sharedDevice);
svgDocument.Root.SetStringAttribute("viewBox", $"0 0 {MyInkConntrol.RenderSize.Width} {MyInkConntrol.RenderSize.Height}");
foreach (var stroke in MyInkConntrol.InkPresenter.StrokeContainer.GetStrokes())
{
var canvasGeometry = CanvasGeometry.CreateInk(session, new[] { stroke }).Outline();
var pathReceiver = new CanvasGeometryToSvgPathReader();
canvasGeometry.SendPathTo(pathReceiver);
var element = svgDocument.Root.CreateAndAppendNamedChildElement("path");
element.SetStringAttribute("d", pathReceiver.Path);
var color = stroke.DrawingAttributes.Color;
element.SetColorAttribute("fill", color);
}
await svgDocument.SaveAsync(randomAccessStream);
}
}
}
private async void save(object sender, RoutedEventArgs e)
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
savePicker.FileTypeChoices.Add("svg file", new List<string>() { ".svg" });
savePicker.SuggestedFileName = "NewSvgfile1";
var file = await savePicker.PickSaveFileAsync();
if (file != null)
{
using (var writeStream = (await file.OpenStreamForWriteAsync()).AsRandomAccessStream())
{
await RenderSvg(writeStream);
await writeStream.FlushAsync();
}
}
}
}
Custom class:
public class CanvasGeometryToSvgPathReader: ICanvasPathReceiver
{
private readonly Vector2 _ratio;
private List<string> Parts { get; }
public string Path => string.Join(" ", Parts);
public CanvasGeometryToSvgPathReader() : this(Vector2.One)
{ }
public CanvasGeometryToSvgPathReader(Vector2 ratio)
{
_ratio = ratio;
Parts = new List<string>();
}
public void BeginFigure(Vector2 startPoint, CanvasFigureFill figureFill)
{
Parts.Add($"M{startPoint.X / _ratio.X} {startPoint.Y / _ratio.Y}");
}
public void AddArc(Vector2 endPoint, float radiusX, float radiusY, float rotationAngle, CanvasSweepDirection sweepDirection, CanvasArcSize arcSize)
{
}
public void AddCubicBezier(Vector2 controlPoint1, Vector2 controlPoint2, Vector2 endPoint)
{
Parts.Add($"C{controlPoint1.X / _ratio.X},{controlPoint1.Y / _ratio.Y} {controlPoint2.X / _ratio.X},{controlPoint2.Y / _ratio.Y} {endPoint.X / _ratio.X},{endPoint.Y / _ratio.Y}");
}
public void AddLine(Vector2 endPoint)
{
Parts.Add($"L {endPoint.X / _ratio.X} {endPoint.Y / _ratio.Y}");
}
public void AddQuadraticBezier(Vector2 controlPoint, Vector2 endPoint)
{
//
}
public void SetFilledRegionDetermination(CanvasFilledRegionDetermination filledRegionDetermination)
{
//
}
public void SetSegmentOptions(CanvasFigureSegmentOptions figureSegmentOptions)
{
//
}
public void EndFigure(CanvasFigureLoop figureLoop)
{
Parts.Add("Z");
}
}
In the end I was able to use the XmlWriter class to write my own canvas-to-svg converter. Using the example in the question:
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Svg;
using Microsoft.Graphics.Canvas.UI.Xaml;
using System;
using System.Xml;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
namespace MyApp
{
public class ExportSVG
{
private XmlWriter Writer { get; }
public SVGWriter(System.IO.Stream stream)
{
Writer = XmlWriter.Create(stream, new XmlWriterSettings()
{
Indent = true,
});
Writer.WriteStartElement("svg", "http://www.w3.org/2000/svg");
Write("version", "1.1");
}
public void AddCanvases(UIElement element)
{
if (element is Grid grid)
{
foreach (UIElement child in grid.Children)
{
AddCanvases(child);
}
}
else if (element is Canvas canvas)
{
AddCanvas(canvas);
}
}
public void AddCanvas(Canvas canvas)
{
foreach (UIElement element in canvas.Children)
{
if (element is Path path)
{
else if (path.Data is EllipseGeometry ellipseGeometry)
{
Writer.WriteStartElement("ellipse");
Write("stroke", ellipseGeometry.Stroke);
Write("stroke-width", ellipseGeometry.StrokeThickness);
Write("cx", ellipseGeometry.Center.X);
Write("cy", ellipseGeometry.Center.Y);
Write("rx", ellipseGeometry.RadiusX);
Write("ry", ellipseGeometry.RadiusY);
Writer.WriteEndElement();
}
}
else if (element is TextBlock textBlock)
{
Writer.WriteStartElement("text");
Write("x", Canvas.GetLeft(textBlock));
Write("y", Canvas.GetTop(textBlock) + textBlock.ActualHeight);
Write("font-family", textBlock.FontFamily.Source);
Write("font-size", $"{textBlock.FontSize}px");
Writer.WriteString(textBlock.Text);
Writer.WriteEndElement();
}
}
}
private void Write(string name, string value)
{
Writer.WriteAttributeString(name, value);
}
private void Write(string name, double value)
{
Write(name, ((float)value).ToString());
}
public void Dispose()
{
Writer.WriteEndElement();
Writer.Close();
Writer.Dispose();
}
}
}
I've some hard time trying to display images in my UWP App.
My images are located in the Images library.
The question is:
How to show jpg in the Image Xaml control on a UWP, that is not in the projects "Assets"?
So far using Binding, I’ve
Try to set Image.Source to the full path with no success, image control remains blank. (for file access retriction a guess)
Try using setting BitMapImage to the Image.Source property with no success, image control remains blank. (for file access retriction a guess)
Try to assign FileStorage class to the Image.Source property with no success, image control remains blank.
Try to load the image file in a stream and load the stream in the BitmapImage, but there I’ve threading issues. More about this below.
About the threading issue, I’ve come to two issues for now.
When loading the file/stream:
var file = File.ReadAllBytes(_currentImagePath);
I get
System.InvalidOperationException: 'Synchronous operations should not be performed on the UI thread. Consider wrapping this method in Task.Run.'
When using Task.Run on any manner I get an exception when trying to set the Bitmap source
The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
var bitmap = new BitmapImage();
return await Task.Run(async () =>
{
var file = File.ReadAllBytes(_currentImagePath);
using (var stream = new InMemoryRandomAccessStream())
{
await stream.WriteAsync(file.AsBuffer());
stream.Seek(0);
await bitmap.SetSourceAsync(stream); // **Exception here**
return bitmap;
}
});
Here the full source code:
namespace App1.Images
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media.Imaging;
using App1.Annotations;
public class ImagesLibrairies : INotifyPropertyChanged
{
private readonly HashSet<Image> _images;
private readonly string _fileTypes;
private readonly HashSet<Image> _preferrednessFiles;
private readonly DispatcherTimer _timer;
private ulong _minFileSize = 5 * 1024; // 5k in bytes
private ulong _maxFileSize = 1024 * 1024 * 20; //20 MBytes
private int _imageIndex = 0;
public ImagesLibrairies()
{
_preferrednessFiles = new HashSet<Image>();
_fileTypes = "*.jpg;*.jpeg;*.bmp;*.tif"; // ignore *.ico;*.png;*.svg;*.gif
_images = new HashSet<Image>();
_timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 5) };
_timer.Tick += _timer_Tick;
}
private Uri _currentImage;
public Uri CurrentImage
{
get => _currentImage;
private set
{
_currentImage = value;
OnPropertyChanged(nameof(CurrentImage));
OnPropertyChanged(nameof(Image));
}
}
public async Task<BitmapImage> GetBitmapImage()
{
var bitmap = new BitmapImage();
return await Task.Run(async () =>
{
var file = File.ReadAllBytes(_currentImagePath);
using (var stream = new InMemoryRandomAccessStream())
{
await stream.WriteAsync(file.AsBuffer());
stream.Seek(0);
await bitmap.SetSourceAsync(stream);
return bitmap;
}
});
}
public BitmapImage Image
{
get
{
if (!string.IsNullOrEmpty(_currentImagePath))
return GetBitmapImage().Result;
return null;
}
}
private string _currentImagePath;
public string CurrentImagePath
{
get => _currentImagePath;
set
{
_currentImagePath = value;
OnPropertyChanged(nameof(CurrentImagePath));
CurrentImage = new Uri(value);
}
}
public object CurrentStorageFile { get; set; }
private void _timer_Tick(object sender, object e)
{
SetNext();
}
public async Task ScanLibrairies()
{
var picturesFolder = KnownFolders.PicturesLibrary;
var files = await picturesFolder.GetFilesAsync();
foreach (var file in files)
{
Debug.WriteLine(file.Name + ", " + file.DateCreated);
if (_images.Count >= int.MaxValue)
return;
var prop = await file.GetBasicPropertiesAsync();
var imageLocation = new Image
{
StorageFile = file,
Properties = prop
};
if (imageLocation.Properties.Size < _minFileSize || imageLocation.Properties.Size > _maxFileSize)
continue;
if (_preferrednessFiles.Any(o => o.Equals(imageLocation) && o.Preferredness == Preferredness.No))
continue;
_images.Add(imageLocation);
}
}
public void SetNext()
{
if (_images == null || !_images.Any())
{
return;
}
_imageIndex++;
var image = _images.Skip(_imageIndex).FirstOrDefault();
if (image != null)
{
Debug.WriteLine($"Displaying: {image.StorageFile.Path}");
}
CurrentImagePath = image?.StorageFile.Path;
CurrentImage = new Uri(CurrentImagePath);
}
public void SetPrevious()
{
if (_images == null || !_images.Any())
return;
_imageIndex--;
var image = _images.Skip(_imageIndex).FirstOrDefault();
CurrentImagePath = image?.StorageFile.Path;
}
public void LikeThisPicture(Image picture)
{
var pic = _preferrednessFiles.FirstOrDefault(o => o.Equals(picture));
if (pic == null)
{
picture.Preferredness = Preferredness.Very;
_preferrednessFiles.Add(picture);
return;
}
pic.Preferredness = Preferredness.Very;
}
public void DislikeThisPicture(Image picture)
{
var pic = _preferrednessFiles.FirstOrDefault(o => o.Equals(picture));
if (pic == null)
{
picture.Preferredness = Preferredness.No;
_preferrednessFiles.Add(picture);
return;
}
pic.Preferredness = Preferredness.No;
}
public void StartAsync()
{
ScanLibrairies();
_timer.Start();
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Here the xaml markup
<Page.DataContext>
<images:ImagesLibrairies/>
</Page.DataContext>
<Grid >
<!--Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">-->
<Image Stretch="UniformToFill" Margin="15">
<Image.Source>
<BitmapImage UriSource="{Binding Image}"></BitmapImage>
<!--<BitmapImage UriSource="{Binding CurrentImagePath}"></BitmapImage>-->
<!--<BitmapImage UriSource="D:\Utilisateurs\hugod\OneDrive\Images\jouets\20180106_132041.jpg"></BitmapImage>-->
<!--<BitmapImage UriSource="Assets/Black-And-White-Wall-Brick-Texture-Shadow-WallpapersByte-com-3840x2160.jpg"></BitmapImage>-->
<!--<BitmapImage UriSource="{Binding CurrentStorageFile}"></BitmapImage>-->
</Image.Source>
</Image>
</Grid>
Raymond is right!
CurrentImage = new BitmapImage();
await CurrentImage.SetSourceAsync(await image.StorageFile.OpenAsync(FileAccessMode.Read));
A goody for Raymond!
I have a static class that contains a dictionary of file extensions and BitMapSource objects. That class contains a function that given a FileInfo object will return the associated BitMapSource object, if it's not already in the dictionary, it will get it and put it in the dictionary before returning it.
This works fine when executed from the GUI thread. However, when I try to put it in a background thread I am not getting anything back. Is there any reason that I shouldn't be able to execute this from a background thread?
Static Class
namespace Test.Classes
{
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
public static class IconMap
{
private static Dictionary<string, BitmapSource> iconDictionary = new Dictionary<string, BitmapSource>();
public static BitmapSource GetFileIcon(FileInfo fileInfo)
{
if (iconDictionary.ContainsKey(fileInfo.Extension))
{
return iconDictionary[fileInfo.Extension];
}
else
{
lock (iconDictionary)
{
Icon icon = Icon.ExtractAssociatedIcon(fileInfo.FullName);
BitmapSource bitMapSource = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions());
iconDictionary.Add(fileInfo.Extension, bitMapSource);
return bitMapSource;
}
}
}
}
}
Control.cs
namespace Test.Controls
{
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using Microsoft.Office.Interop.Outlook;
public partial class AttachedFileInfo : UserControl
{
private FileInfo file;
public AttachedFileInfo(FileInfo fileInfo)
{
this.InitializeComponent();
this.file = fileInfo;
this.FileLink.NavigateUri = new Uri(fileInfo.FullName);
this.FileName.Text = fileInfo.Name;
this.LoadFileIcon(fileInfo);
}
private async void LoadFileIcon(FileInfo fileInfo)
{
Task<BitmapSource> getFileIconTask = Task<BitmapSource>.Factory.StartNew(() =>
{
// If I change this to BitmapSource icon = null; it works as expected.
BitmapSource icon = Classes.IconMap.GetFileIcon(fileInfo);
return icon;
});
await getFileIconTask;
this.FileIcon.Source = Classes.IconMap.GetFileIcon(fileInfo);
// getFileIconTask.Result;
}
private void FileLink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
Process.Start(this.file.FullName);
}
}
}
Your icons are not displayed, because WPF resources can only be used from another thread, if they are freezed by calling .Freeze() method (see code bellow).
Another problem is that your GetFileIcon() method is not thread safe even if you use ConcurrentDictionary, bacase it could still happen that one thread adds icon to iconDictionary and when it leaves locked block of code, another thread could enter locked block and add icon for the same file type to the dictionary, resulting in ArgumentException. So within the lock, you must again test if icon for particulatr extension is not already present in dictionary.
private static ConcurrentDictionary<string, BitmapSource> iconDictionary = new ConcurrentDictionary<string, BitmapSource>();
public static BitmapSource GetFileIcon(FileInfo fileInfo)
{
BitmapSource bitMapSource;
if (iconDictionary.TryGetValue(fileInfo.Extension, out bitMapSource))
{
return bitMapSource;
}
else
{
lock (iconDictionary)
{
if (iconDictionary.TryGetValue(fileInfo.Extension, out bitMapSource))
return bitMapSource;
Icon icon = Icon.ExtractAssociatedIcon(fileInfo.FullName);
bitMapSource = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions());
bitMapSource.Freeze();//Allows BitmapSource to be used on another thread
iconDictionary[fileInfo.Extension] = bitMapSource;
return bitMapSource;
}
}
}
I have an ArrayList which consist of lots of object created by me. I am trying to keep it. As far as I look, the best solution for that is to use binary formatter. But something is wrong with my code. It doesn't work either writing or reading. Here is my code;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private ArrayList allList = new ArrayList();
private FileStream fileStream;
private MemoryStream memoryStream;
public Form1()
{
InitializeComponent();
fileStream = new FileStream("pcdata.dat",FileMode.OpenOrCreate,FileAccess.ReadWrite);
memoryStream = new MemoryStream();
}
public void acceptDevice(Device receivedDevice)
{
//Somehow I call this method
allList.Add(receivedDevice);
saveDeviceDataToFileStream();
}
private void saveDeviceDataToFileStream()
{
SerializeToStream(allList).WriteTo(fileStream);
}
private void loadDeviceDataFromFileStream()
{
fileStream.CopyTo(memoryStream);
allList = (ArrayList)DeserializeFromStream(memoryStream);
}
public static MemoryStream SerializeToStream(object o)
{
MemoryStream stream = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, o);
return stream;
}
public static object DeserializeFromStream(MemoryStream stream)
{
IFormatter formatter = new BinaryFormatter();
stream.Seek(0, SeekOrigin.Begin);
if (null == stream)
{
return null;
}
object o = formatter.Deserialize(stream);
return o;
}
}
}
This is my device class:
namespace WindowsFormsApplication1
{
[Serializable]
public class Device
{
public MyPanel panel; //panel class that I created
public String id;
public int deviceNumber;
public Device(String id, int deviceNumber)
{
panel = new MyPanel();
this.id = id;
this.deviceNumber = deviceNumber;
}
}
}
And this is myPanel class:
namespace WindowsFormsApplication1
{
public class MyPanel : TableLayoutPanel
{
public Panel panel1 = new Panel();
public Panel panel2 = new Panel();
public Panel panel3 = new Panel();
public Panel panel4 = new Panel();
public PictureBox pictureBox1 = new PictureBox();
public Label nameLabel = new Label();
public MyPanel()
{
//...
}
}
}
This is it. When I tried to tun it I get this exception :
SerializationException was unhandled
An unhandled exception of type
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll
Additional information: 'WindowsFormsApplication1, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' Derlemesindeki
'WindowsFormsApplication1.MyPanel' ...
I dont think you will be able to just serialize controls like that. I would suggest creating classes around the data you want to save and make the controls only responsible for display.
[Serializable]
public class MyPanelInfo
{
public PanelInfo panel1;
public PanelInfo panel2;
public PanelInfo panel3;
public PanelInfo panel4;
public Image image;
public string nameLabel;
public MyPanelInfo()
{
}
}
[Serializable]
public class PanelInfo
{
public string id;
public string name;
}
I found this code on StackOverflow. & This code works great for converting a website to image. However, instead of navigating to a new URL, can this code be modified to capture the current website?
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Windows.Forms;
public class WebsiteToImage
{
private Bitmap m_Bitmap;
private string m_Url;
private string m_FileName = string.Empty;
public WebsiteToImage(string url)
{
// Without file
m_Url = url;
}
public WebsiteToImage(string url, string fileName)
{
// With file
m_Url = url;
m_FileName = fileName;
}
public Bitmap Generate()
{
// Thread
var m_thread = new Thread(_Generate);
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();
return m_Bitmap;
}
private void _Generate()
{
var browser = new WebBrowser { ScrollBarsEnabled = false };
browser.Navigate(m_Url);
browser.DocumentCompleted += WebBrowser_DocumentCompleted;
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
browser.Dispose();
}
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Capture
var browser = (WebBrowser)sender;
browser.ClientSize = new Size(browser.Document.Body.ScrollRectangle.Width, browser.Document.Body.ScrollRectangle.Bottom);
browser.ScrollBarsEnabled = false;
m_Bitmap = new Bitmap(browser.Document.Body.ScrollRectangle.Width, browser.Document.Body.ScrollRectangle.Bottom);
browser.BringToFront();
browser.DrawToBitmap(m_Bitmap, browser.Bounds);
// Save as file?
if (m_FileName.Length > 0)
{
// Save
m_Bitmap.SaveJPG100(m_FileName);
}
}
}
public static class BitmapExtensions
{
public static void SaveJPG100(this Bitmap bmp, string filename)
{
var encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
bmp.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParameters);
}
public static void SaveJPG100(this Bitmap bmp, Stream stream)
{
var encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
bmp.Save(stream, GetEncoder(ImageFormat.Jpeg), encoderParameters);
}
public static ImageCodecInfo GetEncoder(ImageFormat format)
{
var codecs = ImageCodecInfo.GetImageDecoders();
foreach (var codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
// Return
return null;
}
}