i am doing a WPF application to import my image from my folder into excel using c#. I had been successful with using microsoft excel but i would like to know if it is possible to view the file if the user does not have microsoft excel installed? Below is the code which i tried converting the image to a String but it does not display in the excel.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ExcelOpenXMLBasics;
//----------------------------------
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Microsoft.Win32;
//----------------------------------
namespace WpfApplication2
{
public partial class Window2 : Window
{
List<String> stringValue;
List<int> intValue;
string Imagefile;
public Window2()
{
InitializeComponent();
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
}
private void btnCreateBasicWorkbook_Click(object sender, RoutedEventArgs e)
{
this.CreateBasicWorkbook("try7.xlsx", true);
}
private void CreateBasicWorkbook(string workbookName, bool createStylesInCode)
{
#region select file
OpenFileDialog fileChooser = new OpenFileDialog();
fileChooser.Filter = "image files (*.png)|*.png|All files (*.*)|*.*";
fileChooser.InitialDirectory = #"C:\";
fileChooser.Title = "Check if the image has been saved into PC disk";
bool? result = fileChooser.ShowDialog();
Imagefile = fileChooser.FileName;
#endregion
#region 1
DocumentFormat.OpenXml.Packaging.SpreadsheetDocument spreadsheet;
DocumentFormat.OpenXml.Spreadsheet.Worksheet worksheet;
System.IO.StreamReader styleXmlReader;
string styleXml;
spreadsheet = Excel.CreateWorkbook(workbookName);
if (spreadsheet == null)
{
return;
}
if (createStylesInCode)
{
Excel.AddBasicStyles(spreadsheet);
}
else
{
using (styleXmlReader = new System.IO.StreamReader("PredefinedStyles.xml"))
{
styleXml = styleXmlReader.ReadToEnd();
Excel.AddPredefinedStyles(spreadsheet, styleXml);
}
}
Excel.AddWorksheet(spreadsheet, "Test 1");
worksheet = spreadsheet.WorkbookPart.WorksheetParts.First().Worksheet;
#endregion
//BitmapImage _Image = new BitmapImage(new Uri(#"C:\Users\ifcdu1\Desktop\limin.PNG"));
//string _img = _Image.ToString();
//#"C:\Users\ifcdu1\Desktop\limin.PNG"
//byte imageArray1 = File.ReadAllBytes(#"C:\Users\ifcdu1\Desktop\limin.PNG");
byte[] imageArray = System.IO.File.ReadAllBytes(Imagefile);
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
//string test = Imagefile;
//byte[] bytes = System.Text.ASCIIEncoding.ASCII.GetBytes(test);
//string base64String = System.Convert.ToBase64String(bytes);
//Console.WriteLine("Base 64 string: " + base64String);
Excel.SetCellValue(spreadsheet, worksheet, 1, 1, base64ImageRepresentation, true);
#region 2
worksheet.Save();
spreadsheet.Close();
System.Diagnostics.Process.Start(workbookName);
#endregion
}
}
}
So if the user has Excel the image will show. Make it so that if the image does not show (there is no Excel) the image opens in something else like pain, or whatever program you want the image to be in.
Related
I've made a WindowsForms program:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace FormChooseFolder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog
{
InitialDirectory = #"C:\",
Title = "Browse Text Files",
CheckFileExists = true,
};
this.openFileDialog1.Multiselect = true;
this.openFileDialog1.Title = "Select Files";
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
TextWriter txt = new StreamWriter("C:\\test.txt");
txt.Write(dr);
txt.Close();
}
}
}
}
Which should write the path of the file chosen to test.txt but it instead writes OK.
Is there any way I can make it show the path of the chosen file like this C:\Pictures\banana.png ?
Instead of storing object of DialogResult to test.txt store FileName selected by user like,
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
TextWriter txt = new StreamWriter("C:\\test.txt");
txt.Write(dr.FileName); //Use FileName property to get entire file path
txt.Close();
}
MSDN documentation: FileDialog.FileName Property
Gets or sets a string containing the file name selected in the file
dialog box.
Why I am getting the error "the name 'imagePdfDocument' does not exist in current context."
Can anyone tell me how to fix it and why is this error message showing..
Thanks in advance.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using C1.Win.C1Tile;
using System.Diagnostics;
private void _exportImage_Click(object sender, EventArgs e)
{
List<Image> images = new List<Image>();
foreach (Tile tile in _imageTileControl.Groups[0].Tiles)
{
if (tile.Checked)
{
images.Add(tile.Image);
}
}
ConvertToPdf(images);
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.DefaultExt = "pdf";
saveFile.Filter = "PDF files (*.pdf)|*.pdf*";
if (saveFile.ShowDialog() == DialogResult.OK)
{
imagePdfDocument.Save(saveFile.FileName);
}
}
private void ConvertToPdf(List<Image> images)
{
RectangleF rect = imagePdfDocument.PageRectangle;
bool firstPage = true;
foreach (var selectedimg in images)
{
if (!firstPage)
{
imagePdfDocument.NewPage();
}
firstPage = false;
rect.Inflate(-72, -72);`enter code here`
imagePdfDocument.DrawImage(selectedimg, rect);
}
}
Look what you have to do..... go to reference option of your project and uninstall the one which are identical, there may be same reference with different version.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace StopwatchTimer
{
public partial class Form1 : Form
{
public string settingsPath = "Settings";
private string settingsFileName = "settings.txt";
private static readonly Stopwatch watch = new Stopwatch();
private long diff = 0, previousTicks = 0, ticksDisplayed = 0;
public Form1()
{
InitializeComponent();
richTextBox1.TabStop = false;
richTextBox1.ReadOnly = true;
richTextBox1.BackColor = Color.White;
richTextBox1.Cursor = Cursors.Arrow;
richTextBox1.Enter += RichTextBox1_Enter;
settingsPath = Path.Combine(Path.GetDirectoryName(Application.LocalUserAppDataPath), settingsPath);
if (!Directory.Exists(settingsPath))
Directory.CreateDirectory(settingsPath);
settingsFileName = Path.Combine(settingsPath, settingsFileName);
if (!File.Exists(settingsFileName))
File.Create(settingsFileName);
string[] settings = File.ReadAllText(settingsFileName).Split(',');
if(settings.Length > 0)
{
}
} radioButton1.Checked = true;
}
I didn't write yet to the text file. I want for example to write to the text file the radioButton1 and radioButton2 states somewhere else in the code and then reading the states back when running the application.
So I'm using Split with ',' but how do I check if the value when reading back is belong to the radioButton1 or radioButton2 or maybe a textBox or a button1 ?
You can use the built in winforms settings:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Username = txtUsername.Text;
Properties.Settings.Default.Password = txtPassword.Text;
Properties.Settings.Default.Save();
}
C# Setting load and save
https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-write-user-settings-at-run-time-with-csharp
i have problem in my code
i am trying to read text from arabic rtl file using itextsharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
namespace Masmo3
{
public partial class FrmMain : Form
{
string book = null;
public FrmMain()
{
InitializeComponent();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
BtnOpen.Enabled = false;
book = null;
using(OpenFileDialog Ofd = new OpenFileDialog() {Filter= "PDF files (*.pdf)|*.pdf|txt files (*.txt)|*.txt",ValidateNames= true, Multiselect= false})
{
if (Ofd.ShowDialog() == DialogResult.OK)
{
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(Ofd.FileName);
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
sb.Append(PdfTextExtractor.GetTextFromPage(reader, i));
book = sb.ToString();
}
reader.Close();
}
}
BtnOpen.Enabled = true;
}
}
}
this code show my text like "ﻢﯿﺣﺮﻟا ﻦﻤﺣﺮﻟا ﷲ ﻢﺴﺑ" its wrong because its rtl language the correct way "بسم الله الرحمن الرحيم"
what i can do to get correct text direction ?
"بسم الله الرحمن الرحيم"
please use ColumnText object to set the DIrection to be RTL
CT.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
Good luck bro
I know how to upload a video to Youtube using Youtube API within C#
But I'd like to use the Youtube API to return a date when a particular user last uploaded a video.
My code for uploading a video using C# is below, but I really don't know how to do the above???
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Google.GData.Client;
using Google.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.GData.YouTube;
using Google.GData.Extensions.Location;
namespace UploadLimit
{
public partial class Form1 : Form
{
string key = "somegarbage";
string appName = "SlowUpload";
string username = "blarg";
string password = "blarg";
public Form1()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
DialogResult result = dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
txtFile.Text = dialog.FileName;
}
private void btnUpload_Click(object sender, EventArgs e)
{
YouTubeRequestSettings settings = new YouTubeRequestSettings(appName, key, username, password);
YouTubeRequest request = new YouTubeRequest(settings);
Video newVideo = new Video();
newVideo.Title = "My Test Movie";
newVideo.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema));
newVideo.Keywords = "cars, funny";
newVideo.Description = "My description";
newVideo.YouTubeEntry.Private = false;
newVideo.Tags.Add(new MediaCategory("mydevtag, anotherdevtag",
YouTubeNameTable.DeveloperTagSchema));
newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);
// alternatively, you could just specify a descriptive string
// newVideo.YouTubeEntry.setYouTubeExtension("location", "Mountain View, CA");
newVideo.YouTubeEntry.MediaSource = new MediaFileSource(txtFile.Text, "video/quicktime");
Video createdVideo = request.Upload(newVideo);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UploadLimit
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
You can get all the user's videos and get the latest date:
var videos = new YouTubeRequest().GetVideoFeed(userId).Entries
DateTime lastUploadDate = videos.Max(video => video.YouTubeEntry.Published)
To get the actual video's title:
var lastVideo = videos.Where(video => video.YouTubeEntry.Published == lastUploadDate).First();
var name = lastVideo.Title