how i pass image in This function - c#

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension
Nullable<bool> result = dlg.ShowDialog(); // Show open file dialog box
// Process open file dialog box results
if (result == true)
{
// Open document
string filename = dlg.FileName;
txtBrowse.Text = filename;
}
ImageSource imageSource = new BitmapImage(new Uri(txtBrowse.Text));
imgImageAdlut.Source = imageSource; ;
byte[] array1 = null;
//array1 = imageToByteArray();
}
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}

Try this:
var img = new System.Drawing.Bitmap(#"D:\Your image path\your image.bmp");
byte[] array1 = imageToByteArray(img);

Related

C# How to load a Canvas with XML

my problem is that I can't figure out how to load a Canvas type object from an XML file. I hope you can help me.
This is what I was trying, I'll show you the code of how I'm doing the serialization too, just in case I did something wrong.
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.FileName = "untitled";
sfd.Filter = "XML Files(*.xml) | *.xml | Text Files(*.txt) | *.txt | All Files(*.*) | *.* ";
sfd.ShowDialog();
MainWindow mainWindow = new MainWindow();
SerializeToXML(mainWindow, designSpace, 96, sfd.FileName);
mainWindow.Close();
}
public static void SerializeToXML(MainWindow window, Canvas canvas, int dpi, string filename)
{
string mystrXAML = XamlWriter.Save(canvas);
FileStream filestream = File.Create(filename);
StreamWriter streamwriter = new StreamWriter(filestream);
streamwriter.Write(mystrXAML);
streamwriter.Close();
filestream.Close();
}
private void LoadButton_Click(object sender, RoutedEventArgs e)
{
var fileContent = string.Empty;
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = "c:\\";
ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
ofd.FilterIndex = 2;
ofd.RestoreDirectory = true;
ofd.ShowDialog();
var fileStream = ofd.OpenFile();
string boi = ofd.FileName;
// Create an instance of the XmlSerializer specifying type.
XmlSerializer serializer =
new XmlSerializer(typeof(Canvas));
// Create a TextReader to read the file.
FileStream fs = new FileStream(boi, FileMode.Open);
TextReader reader = new StreamReader(fs);
// Use the Deserialize method to restore the object's state.
designSpace = (Canvas)serializer.Deserialize(reader);
}
The app works well until I try to load an XML file, that's when this exception pops out:
System.InvalidOperationException: 'Error to reflect type System.Windows.Controls.Canvas'.'
NotSupportedException: Can not serialize the member
System.Windows.Input.InputBinding.Command type
System.Windows.Input.ICommand because it's an interface.
I did
in Saving:
string xml = XamlWriter.Save(cssave);
using (StreamWriter fl = new StreamWriter(path, false))
{
fl.Write(xml);
}
In Loading:
string xml = File.ReadAllText(currpath);
//Convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(xml);
MemoryStream stream = new MemoryStream(byteArray);
Canvas cs = (Canvas)XamlReader.Load(stream);
csmain.Children.Add(cs);

Save an image to a specified path with default filename without SaveFileDialog in C#? [duplicate]

This question already has answers here:
How to save a WPF BitmapSource image to a file?
(2 answers)
Closed 5 years ago.
A WPF button opens a file dialog to open an image and display it in an image control
I want to use another button event to open the image in another app, is this possible?
Try this:
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(imagebox.Source as BitmapImage));
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}
if (data != null)
{
string filename = dlg.FileName;
System.IO.File.WriteAllBytes(filename, data);
}
}
If you don't want to use a dialog you could just specify the file name in a string variable:
byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(imagebox.Source as BitmapImage));
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}
if (data != null)
{
const string filename = #"c:\test\1.jpg";
System.IO.File.WriteAllBytes(filename, data);
}

after selecting image display same image inside picturebox

on buttonclick I'm taking image from file system and save into database, everything is ok but I want when I select image to display that image into pictureBox1
OpenFileDialog open = new OpenFileDialog() { Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg" };
if (open.ShowDialog() == DialogResult.OK)
{
txtPhoto.Text = open.FileName;
}
string image = txtPhoto.Text;
Bitmap bmp = new Bitmap(image);
FileStream fs = new FileStream(image, FileMode.Open, FileAccess.Read);
byte[] bimage = new byte[fs.Length];
fs.Read(bimage, 0, Convert.ToInt32(fs.Length));
fs.Close();
byte[] Photo = bimage;
You can use Image property to set the Image for PictureBox Control.
Try This:
DialogResult result= openFileDialog1.ShowDialog();
if(result==DialogResult.OK)
pictureBox1.Image =new Bitmap(openFileDialog1.FileName);
if you want to add it in your code
Complete Code:
OpenFileDialog open = new OpenFileDialog() { Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg" };
if (open.ShowDialog() == DialogResult.OK)
{
txtPhoto.Text = open.FileName;
}
string image = txtPhoto.Text;
Bitmap bmp = new Bitmap(image);
pictureBox1.Image = bmp;//add this line
FileStream fs = new FileStream(image, FileMode.Open, FileAccess.Read);
byte[] bimage = new byte[fs.Length];
fs.Read(bimage, 0, Convert.ToInt32(fs.Length));
fs.Close();
byte[] Photo = bimage;
Simple code:
picturebox.Image = Bitmap.FromFile(yourimagepath);
OpenFileDialog open = new OpenFileDialog()
{
Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg"
};
if (open.ShowDialog() == DialogResult.OK)
{
PictureBoxObjectName.Image = Image.FromFile(open.FileName);
}
openFileDialog1.Multiselect = false;
openFileDialog1.Filter= "jpg files (*.jpg)|*.jpg";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
foreach (String file in openFileDialog1.FileNames)
{
picturebox1.Image = Image.FromFile(File);
}
}
This is for selection of one image.

How to check File Size or dimension?

var dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "(*.JPG;*.GIF)|*.JPG;*.GIF";
dlg.ShowDialog();
if (string.IsNullOrEmpty(dlg.FileName)) return;
var fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read);
var data = new byte[fs.Length];
fs.Read(data, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
I am using this code but not find how to find size or dimension of image?
Answered as a part of this question: Limit image size
string filename = // get it from OpenFileDialog
var length = new FileInfo(filename).Length;
Image img = Image.FromFile(filename);
var w = img.Width;
var h = img.Height;
use FileInfo given path of this file and use Length as
var dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "(*.JPG;*.GIF)|*.JPG;*.GIF";
dlg.ShowDialog();
if (string.IsNullOrEmpty(dlg.FileName))
return;
FileInfo info = new FileInfo(dlg.FileName);
Console.Write("Length In Bytes:"+info.Length);

How to save BitmapImage via SaveFileDialog from WPF?

The way I try implement it via standart Windows.Forms (to get valide DialogResult.OK)
System.Windows.Forms.SaveFileDialog dlg = new System.Windows.Forms.SaveFileDialog();
dlg.FileName = "Document"; // Default file name
// dlg.DefaultExt = ".jpg"; // Default file extension
dlg.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
if(dlg.ShowDialog()== System.Windows.Forms.DialogResult.OK)
if (dlg.DialogResult.HasValue && splashDialog.DialogResult.Value)
{
string fName = dlg.FileName;
if (dlg.FileName != "")
{
System.IO.Stream fileStream = (System.IO.FileStream)dlg.OpenFile();
fileStream.Close();
}
}
This is using Windows forms but it saves blank image((
You can do something like that:
var encoder = new JpegBitmapEncoder(); // Or PngBitmapEncoder, or whichever encoder you want
encoder.Frames.Add(BitmapFrame.Create(yourImage));
using (var stream = dlg.OpenFile())
{
encoder.Save(stream);
}
BTW, there is a SaveFileDialog in WPF too, you don't have to use the one from Windows Forms
For WPF code will be like :
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document";
dlg.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
if (dlg.ShowDialog() == true)
{
var encoder = new JpegBitmapEncoder(); // Or PngBitmapEncoder, or whichever encoder you want
encoder.Frames.Add(BitmapFrame.Create(bi));
using (var stream = dlg.OpenFile())
{
encoder.Save(stream);
}
}
Here bi is the BitmapImage

Categories