How to apply a different text for each image? - c#

I have this looping intruction (foreach) which reads every image from the "GalleryImages" folder. What I want is to apply a different text for each image. Do I do this with the foreach instruction as well? Do I create a folder where I plce more .txt files?
var files = Directory.GetFiles(#".\GalleryImages");
string text22 = System.IO.File.ReadAllText(#"C:\Users\Public\TestFolder\WriteText.txt");
foreach (var file in files)
{
foreach ()
{
FileInfo fileInfo = new FileInfo(file);
WineModel wineModel = new WineModel();
wineModel.Image = new Uri(file, UriKind.Relative);
wineModel.Description = text22 + "text text text text text text text text text text text" +
Environment.NewLine + "text text text text text text text text text text text";
wineModel.Price = new Random().NextDouble();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = wineModel.Image;
bi.EndInit();
var button = new KinectTileButton
{
Label = System.IO.Path.GetFileNameWithoutExtension(file),
Background = new ImageBrush(bi),
Tag = wineModel
};
this.wrapPanel.Children.Add(button);
}
}
I've also posted the code for the click event :
private void KinectTileButtonClick(object sender, RoutedEventArgs e)
{
var button = (KinectTileButton)e.Source;
var wineModel = button.Tag as WineModel;
var selectionDisplay = new SelectionDisplay(wineModel);
this.kinectRegionGrid.Children.Add(selectionDisplay);
e.Handled = true;
}

Related

Wpf Replace Text into RichTextBox

I work in WPF C#, i want to replace text into RichtextBox
i load my rtf File who contain picture it's work fine.
If i use this :
ReposLettresFusion m_ReposLettresFusion = new ReposLettresFusion();
TextRange range = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
FileStream fStream = new FileStream(#pPath, FileMode.Create);
range.Text = m_ReposLettresFusion.Fusion(pPkGuidLettre, range.Text);
range.Save(fStream, DataFormats.Rtf);
fStream.Close();
rtb.LoadRtf(pPath);
m_ReposLettresFusion = null;
range = null;
It's change my text but i loose my picture and fonts all my formatting.
How i can replace text and keep all formatting of the Rtf
Thank you
Try this :
public void Fusion(ref Xceed.Wpf.Toolkit.RichTextBox pRichTextControl)
{
foreach (KeyValuePair<string, string> entry in LettreFusion)
{
string keyword = entry.Key;
string newString = entry.Value;
TextRange text = new TextRange(pRichTextControl.Document.ContentStart, pRichTextControl.Document.ContentEnd);
TextPointer current = text.Start.GetInsertionPosition(LogicalDirection.Forward);
while (current != null)
{
string textInRun = current.GetTextInRun(LogicalDirection.Forward);
if (!string.IsNullOrWhiteSpace(textInRun))
{
int index = textInRun.IndexOf(keyword);
if (index != -1)
{
TextPointer selectionStart = current.GetPositionAtOffset(index, LogicalDirection.Forward);
TextPointer selectionEnd = selectionStart.GetPositionAtOffset(keyword.Length, LogicalDirection.Forward);
TextRange selection = new TextRange(selectionStart, selectionEnd);
selection.Text = newString;
pRichTextControl.Selection.Select(selection.Start, selection.End);
pRichTextControl.Focus();
}
}
current = current.GetNextContextPosition(LogicalDirection.Forward);
}
}
}

Identify the text pattern on image and get the string values to a text file

I am working on an image which has the text on it.
Text example: X4 200 , X3 400, X8 2o and the same type of text present on image.
I need to read the X4 and corresponding value from C#,and then i need to save those values in a text file.
Please let me know how can i start with it.
I am trying with this code on button click event.
' OpenFileDialog ofd = new OpenFileDialog();
if(ofd.ShowDialog()==DialogResult.OK)
{
pictureBox1.Image =(Bitmap)Bitmap.FromFile(ofd.FileName);
}
using (var tEngine = new TesseractEngine("./tessdata", "eng", EngineMode.TesseractAndCube))
{
using (var img = Pix.LoadFromFile(ofd.FileName))
{
using (var page = tEngine.Process(img))
{
var text = page.GetText();
textBox1.Text = text;
}
}
'
image is here [1]: https://i.stack.imgur.com/PgcpZ.png

C#: Unable to assign Image to PictureBox image source path from text file in winforms

I'm trying to display image in a PictureBox dynamically. Image source is stored in a text file. When reading the image path from the file it keep showing image error symbol. When I include the path in the code it works.
Text file line sample
F01,Nasi Lemak,RM 2,#"Food\NasiLemak.jpg"
public void readData()
{
try
{
int i = 0;
foreach (string line in File.ReadAllLines("food.txt"))
{
string[] parts = line.Split(',');
foreach (string part in parts)
{
Console.WriteLine("{0}:{1}", i, part);
{
Label LblFId = new Label();
{
//LblFId.AutoSize = true;
LblFId.Size = new System.Drawing.Size(70, 20);
}
Label LblFName = new Label();
{
LblFName.Size = new System.Drawing.Size(70, 20);
}
Label LblFPrice = new Label();
{
LblFPrice.Size = new System.Drawing.Size(70, 20);
}
PictureBox foodPicBox = new PictureBox();
{
foodPicBox.Size = new System.Drawing.Size(200, 200);
foodPicBox.SizeMode = PictureBoxSizeMode.StretchImage;
foodPicBox.BorderStyle = BorderStyle.Fixed3D;
}
Panel fPanel = new Panel();
LblFId.Text = parts[0];
LblFName.Text = parts[1];
LblFPrice.Text = parts[2];
foodPicBox.ImageLocation = parts[3];
fPanel.Controls.Add(LblFId);
fPanel.Controls.Add(LblFName);
fPanel.Controls.Add(LblFPrice);
fPanel.Controls.Add(foodPicBox);
foodFlow.Controls.Add(fPanel);
}
}
i++;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
The problem is in the path from the text file
#"Food\NasiLemak.jpg"
This should be saved like without the # and the "
Food\NasiLemak.jpg
Or you should write more code to remove those symbols like this
foodPicBox.ImageLocation = parts[3].Replace("#", "").Replace("\"", "");
This will remove the samples and your problem would be solved.
You need also to close foreach statment at this point
foreach (string part in parts)
{Console.WriteLine("{0}:{1}", i, part);}

Programatically set the background of a Node image to a color

The code below generates a treeview node with an Icon (from a .PNG file Piconfolder\PiconName) followed by a Text string Pheader.
I want to add a background to just the icon (to be used with transparent icons)
public static TreeViewItem CreateTreeViewItem(
string Pheader,
string PiconFolder,
string PiconName)
{
string iconFolder = PiconFolder;
string iconName = PiconName;
string header = Pheader;
TreeViewItem child = new TreeViewItem();
StackPanel pan = new StackPanel();
pan.Orientation = Orientation.Horizontal;
string fPath = System.IO.Path.Combine(iconFolder, iconName);
Image image = new Image();
image.Height = 16;
image.Width = 16;
image.Source = new BitmapImage(new Uri(fPath, uriKind.RelativeOrAbsolute));
pan.Children.Add(image);
pan.Children.Add(new TextBlock(new Run(" " + header)));
child.Header = pan;
}
Replace
pan.Children.Add(image);
with
var iconPanel = new Grid(); // or other panel
iconPanel.Background = Brushes.Blue;
iconPanel.Children.Add(image);
pan.Children.Add(iconPanel);

why element.Text= not work

Try to do next - i want to check if some information is already exist if csv file, if yes - open form with label, and put to this label information from file
Code is next:
public void getEventTime(string filePath, string currDate, string currentDateTimeHM)
{
//reading the *.csv file and convert to the array of data
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
//create array for getting any vlaue from string
string[] arrOfData = sr.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
sr.Close();
List<string> lines = new List<string>();
bool status=false;//variable for showing form if event exist
foreach (var l in arrOfData)
{
if (l.Contains(currDate) && l.Contains(currentDateTimeHM))
{
string[] temp = l.Split(',').Take(5).ToArray();
notLabel.Text = temp[1].ToString();
status = true;
}
}
if (status)
{
//show Notification Form
Form NotificationForm = new Notification();
NotificationForm.Visible = true;
}
}
all works perfect - if information exist - new form opens, but notLabel.Text = temp[0].ToString(); this part have return nothing. During debuging i got next
means that code is correct but in strange for me reason result in programm - without this text.
where I make some mistake?
Below form with label
checked
few rows from file NotificationDesigner.Form.cs
this.notLabel.AutoSize = true;
this.notLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.notLabel.Location = new System.Drawing.Point(12, 22);
this.notLabel.Name = "notLabel";
this.notLabel.Size = new System.Drawing.Size(34, 13);
this.notLabel.TabIndex = 0;
this.notLabel.Text = "label";
where do you call the method getEventTime and what is notLabel.
if the method getEventTime is called and set the notLabel.Text but after that the text is set again to string.Empty that there is the problem, so you should probably search or debug every change to notLabel.Text.
are you sure the it is notLabel the it is shown in the form? you can check that by registering to the mouseDown event and see that it is called when you click on the Label
one more thing, add break;
after your line
status = true;
go to the design and press the label, press F4 and search the name property, i bet it is not notLabel:
EDIT
i think i fount your problem
correct me if i'm wrong but this lines
if (status)
{
//show Notification Form
Form NotificationForm = new Notification();
NotificationForm.Visible = true;
}
are happening after you change the text... when what you meant is:
public void getEventTime(string filePath, string currDate, string currentDateTimeHM)
{
Form NotificationForm = new Notification();
//reading the *.csv file and convert to the array of data
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
//create array for getting any vlaue from string
string[] arrOfData = sr.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
sr.Close();
List<string> lines = new List<string>();
bool status=false;//variable for showing form if event exist
foreach (var l in arrOfData)
{
if (l.Contains(currDate) && l.Contains(currentDateTimeHM))
{
string[] temp = l.Split(',').Take(5).ToArray();
NotificationForm.NotText = temp[1].ToString();
status = true;
}
}
if (status)
{
//show Notification Form
NotificationForm.Visible = true;
}
}
and in the notification form do
public string NotText
{
set { notLabel.Text = value; }
}

Categories

Resources