I am developing a desktop application using wpf.I will need to store some pictures and show them after.I need a textbox with the link to the database,and that will be saved in a database.They told me that the best approach is to save a image path to the database,not the image itself.
Is that better then blob?I can't find any example with the path on the new,only with blob...
You can save image to the database as a link, but beforehand rename it to GUID to ensure uniqueness of reference you will be storing
/this is my code , and you can save an image into datebase/
namespace WpfApp1
{
public partial class saveImage : Window
{
public saveImage()
{
InitializeComponent();
}
OpenFileDialog Op = new OpenFileDialog();
private void Button_Click(object sender, RoutedEventArgs e)
{
Op.Title = "image selection";
Op.Filter = "JPG(.jpg)|*.jpg|PNG(.png)|*.png|JPEG(.jpeg)|*.jpeg";
if (Op.ShowDialog() == true)
{
img1.Source = new BitmapImage(new Uri(Op.FileName));
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
PersonAccountingdbEntities db = new PersonAccountingdbEntities();
tbl_Setting set = new tbl_Setting();
set.Image1 = File.ReadAllBytes(Op.FileName);
db.tbl_Setting.Add(set);
db.SaveChanges();
MessageBox.Show("image has been saved");
}
}
}
Related
Goal: Direct print a CSV file which is selected from another button on my forms application
Problem: I don't know how to tell amy btnPrintFile_click method the FileName coming from another method in form1.cs
Any help would be appreciated, Im new to forms in c#
public void openCVSFile(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = false;
ofd.Filter = "CSV files (*.csv)|*.csv";
ofd.FilterIndex = 1;
if(ofd.ShowDialog() == DialogResult.OK)
{
txtAddressCount.Text = ("Address count: "+ ofd.FileName);
}
}
private void btnPrintFile_Click(object sender, EventArgs e)
{
try
{
streamToPrint = new StreamReader(ofd.FileName);
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
For reference I'm using this article from Microsoft
https://learn.microsoft.com/en-us/dotnet/api/system.drawing.printing.printdocument?redirectedfrom=MSDN&view=dotnet-plat-ext-6.0
public void openCVSFile(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
...
}
...
private void btnPrintFile_Click(object sender, EventArgs e)
{
try
{
streamToPrint = new StreamReader(ofd.FileName);
^^^
ofd was declared in the other method
Im new to forms in c#
This isn't about forms; you can never reach a variable declared inside one method, from another method.. the variable has to be passed from one method to the other, or it has to be declared at a scope that both methods can access.
Declare your OpenFileDialog under the class instead:
class YourForm: Form{
private OpenFileDialog ofd = new OpenFileDialog();
or drop it onto the form in the designer so it appears in the tray under the form view:
And rename it so it is called ofd by altering the (Name) line in the property grid:
Either of these ways make it accessible to all methods in the class. Doing it the visual way probably makes life easier in terms of setting other properties like the initial folder, file filter etc
I would like to clear a pictureBox after clicking on an 'Encode' button to save it as a new image file in my project. I would like to clear the fields on the form after the user saves the image file.
The code I use to display image on pictureBox:
private void btnOpenfile_Click(object sender, EventArgs e)
{
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files (*.png)|*.png";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
pictureBox1.Image = new Bitmap(open.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
tbFilepath.Text = open.FileName;
//pictureBox1.ImageLocation = tbFilepath.Text;
}
}
The code I used to clear the pictureBox:
private void clearForm()
{
pictureBox1.Image = null; //doesn't work
pictureBox1.Invalidate(); //doesn't work
tbFilepath.Text = "";
tbMessage.Text = "";
}
I have also tried the following but it did not work either:
private void clearForm()
{
Bitmap bm = new Bitmap(img);
bm.Save(tbFilepath.Text,System.Drawing.Imaging.ImageFormat.Png);
}
I tried using the Refresh() method as one of the commentors suggested, but it did not work either:
private void clearForm()
{
Refresh(); //first attempt
pictureBox1.Refresh();// second attempt
}
I expect the pictureBox field to clear out the existing image I have selected but the image did not clear away.
Before clicking on encode button
After clicking on encode button, the textBox fields are cleared but not the pictureBox field. I used the codes I have added in this question.
Try creating a new Bitmap Variable and use it for the picturebox:
Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
pictureBox.Image = bitmap;
pictrueBox.Invalidate();
Also, try to declare a Global Variable for the Bitmap so it is the one to be set as the picture and also the one to be cleared before setting it as the image for the PictureBox.
On the other hand you could try using the OnPaint Method of the Picurebox:
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
Bitmap bm = null;
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
pictureBox1.Image = bm;
}
private void btn_Open_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp); *.PNG|*.jpg; *.jpeg; *.gif; *.bmp; *.PNG";
if (ofd.ShowDialog() == DialogResult.OK)
{
bm = new Bitmap(Image.FromFile(ofd.FileName), new Size(pictureBox1.Width, pictureBox1.Height));
textBox1.Text = ofd.FileName;
pictureBox1.Invalidate();
}
}
private void btn_Clear_Click(object sender, EventArgs e)
{
bm = null;
pictureBox1.Invalidate();
}
These code I made worked perfectly. So please try it.
void OpenWithDialog()
{
var ofd = new OpenFileDialog();
ofd.Filter = "Triangle polygon file|*.poly";
if (ofd.ShowDialog() == DialogResult.OK)
{
OpenPolyFile(ofd.FileName);
}
}
void OpenPolyFile(string file)
{
var geometry = TriangleNet.IO.FileReader.ReadPolyFile(file);
// ...
}
private void button1_Click(object sender, EventArgs e)
{
}
How to read files in button1 click directly?
what you want is that your geometry object be accessible in additional scopes besides OpenPolyFile(). So you can simply make the geometry declaration accessible to both methods, declaring it, say, in the form code behind
// class scoped variables
[ThePolyFileType] (pick the right one here :) geometry = null;
void OpenWithDialog()
{
var ofd = new OpenFileDialog();
ofd.Filter = "Triangle polygon file|*.poly";
if (ofd.ShowDialog() == DialogResult.OK)
{
OpenPolyFile(ofd.FileName);
}
}
void OpenPolyFile(string file)
{
geometry = TriangleNet.IO.FileReader.ReadPolyFile(file);
// ...
}
private void button1_Click(object sender, EventArgs e)
{
if (geometry != null)
{
//do your stuff
}
}
How do i read already opened file in a another button click event
directly .(i.e without open file dialogue in button click)
I'm not quite sure why you want to read it twice, but if that requirement says, make selected filename available globally and use it.
private string filename;
void OpenWithDialog()
{
var ofd = new OpenFileDialog();
ofd.Filter = "Triangle polygon file|*.poly";
if (ofd.ShowDialog() == DialogResult.OK)
{
OpenPolyFile(ofd.FileName);
filename = ofd.FileName
}
}
Now you have opened filename available in button_clcik you can use this file and read again.
private void button1_Click(object sender, EventArgs e)
{
// now you can read the file.
//File.ReadAllText(filename); //OR
TriangleNet.IO.FileReader.ReadPolyFile(file);
}
I am trying to put together a form application to browse and play WAV files. Currently, it has two buttons - one to browse and select the WAV, the other one is to play. I have implemented the browse button and it is working ok. I checked it by playing the WAV sound within the button, as you can see:
private void Browse_Click(object sender, EventArgs e) {
OpenFileDialog tarik = new OpenFileDialog();
tarik.Title = "Browse...";
tarik.InitialDirectory = #"Desktop";
tarik.Filter = "Wav files (*.wav)|*.wav";
tarik.RestoreDirectory = true;
if (tarik.ShowDialog() == DialogResult.OK) {
textBox1.Text = tarik.FileName;
Stream tarik2 = tarik.OpenFile();
SoundPlayer snd = new SoundPlayer(tarik2);
snd.Play();
}
}
I tested the code and it is working, but when I try to call the 'tarik' from another button:
private void Play_Click(object sender, EventArgs e) {}
As shown above, it says I am not allowed to do this.
The variables that you create in your browse handler are local variables (as they should be) which means they can't be accessed (because they don't exist) once the method ends.
You'll need to create an instance field, which exists for the entire lifetime of the object, to allow the other method to access it:
//new instance field.
private string tarikFileName;
private void Browse_Click(object sender, EventArgs e)
{
OpenFileDialog tarik = new OpenFileDialog();
tarik.Title = "Browse...";
tarik.InitialDirectory = #"Desktop";
tarik.Filter = "Wav files (*.wav)|*.wav";
tarik.RestoreDirectory = true;
if (tarik.ShowDialog() == DialogResult.OK) {
//store value in instance field
tarikFileName = tarik.FileName;
textBox1.Text = tarik.FileName;
Stream tarik2 = tarik.OpenFile();
using(SoundPlayer snd = new SoundPlayer(tarik2))
snd.Play();
}
}
private void Play_Click(object sender, EventArgs e)
{
if(tarikFileName != null)
{
Stream stream = File.OpenRead(tarikFileName);
using(SoundPlayer snd = new SoundPlayer(stream))
snd.Play();
}
}
Also note that the SoundPlayer should be disposed when you're done with it, so I've wrapped it in a using block to ensure that happens.
I'd suggest the following approach:
Declare the SoundPLayer as a variable of your Form.
In the handler of Browse button get the file name, create a stream and initialize your SoundPlayer with it.
In the handler of Play button call the Play() method of the SoundPlayer.
In order to share data across the two methods you need some place to store the references. In your case I would recommend pulling the file name from textBox1.Text. That way you don't have to worry about managing (opening/closing) the stream in multiple places.
private void Browse_Click(object sender, EventArgs e)
{
OpenFileDialog tarik = new OpenFileDialog();
tarik.Title = "Browse...";
tarik.InitialDirectory = #"Desktop";
tarik.Filter = "Wav files (*.wav)|*.wav";
tarik.RestoreDirectory = true;
if (tarik.ShowDialog() == DialogResult.OK) {
textBox1.Text = tarik.FileName;
}
}
private void Play_Click(object sender, EventArgs e)
{
using(Stream tarik2 = File.Open(textBox1.Text, FileMode.Open))
{
SoundPlayer snd = new SoundPlayer(tarik2);
snd.Play();
}
}
using Telerik.WinControls.Data;
using Telerik.WinControls.UI.Export;
namespace Directory
{
public partial class radForm : Form
{
public radForm()
{
InitializeComponent();
}
private void radForm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'directoryDataSet.DirDetails' table. You can move, or remove it, as needed.
this.dirDetailsTableAdapter.Fill(this.directoryDataSet.DirDetails);
}
// Button click
private void button1_Click(object sender, EventArgs e)
{
ExportToPDF exporter = new ExportToPDF(this.radGridView1);
//The FileExtension property allows you to change the default (*.pdf) file extension of the exported file
exporter.FileExtension = "pdf";
exporter.HiddenColumnOption = Telerik.WinControls.UI.Export.HiddenOption.DoNotExport;
// This to make the grid fits to the PDF page width
exporter.FitToPageWidth = true;
// Exporting data to PDF is done through the RunExport method of ExportToPDF object
string fileName = "c:\\Directory-information.pdf";
exporter.RunExport(fileName);
}
}
}
Some how Im missing something here and my gridview isn't exporting into pdf and no file creation takes place.
private void button1_Click(object sender, EventArgs e)
{
ExportToPDF exporter = new ExportToPDF(this.radGridView1);
exporter.FileExtension = "pdf";
exporter.HiddenColumnOption = Telerik.WinControls.UI.Export.HiddenOption.DoNotExport;
exporter.ExportVisualSettings = true;
exporter.PageTitle = "Directory Details";
exporter.FitToPageWidth = true;
string fileName = "c:\\Directory-information.pdf";
exporter.RunExport(fileName);
MessageBox.Show("Pdf file created , you can find the file c:\\Directory-informations.pdf");
}