In the below For loop, after the loop has finished, it increments the counter one last time e.g. if the condition is i<2, it will increment to 2 before exiting the loop. The images list will then try to use the value images[2] which doesn't exist. This will result in images not displaying
I know there are other questions on using an anonymous method within a loop and I've applied the recommended solutions, but no such luck.
This is my code behind
FileModel fileModel = DependencyService.Get<IImageFetcher>().ReadImagesAsync();
var items = new List<FileImageInfo>();
for (int i = 0; i < fileModel.Images.Count; i++)
{
var imageStream = ImageSource.FromStream(() => new MemoryStream(fileModel.Images[i]));
items.Add(new FileImageInfo
{
FileType = "Jpeg",
FileSize = fileModel.Sizes[i],
DateCreated = fileModel.Dates[i],
imageSource = imageStream
});
}
Items.ItemsSource = items;
the above code works indicating the issue is definitely with the loop and not with the images or anything else
Using a foreach loop did the trick. Was told over on the Xamarin forum that have to use foreach not For with listview images
Related
I would like to like the WPF application saves time similar to the one in the rankings. Time and number of attempts is not a problem to list but the problem occurs when I want to sort it in a text file. The user will be able to turn on the leaderboard in a text file as well as when the application is completed, the chart (text file) will open and show the user's time.
private void writeText(string strPath, TimeSpan tsDuration)
{
using (StreamWriter str = new StreamWriter(strPath, true))
{
str.WriteLine(tsDuration / suma);
//duration= time of the game
//suma= number of attempts
}
}
readonly string path = #"C:\Users\info\Desktop\žebříček.txt";
TimeSpan duration = TimeSpan.FromMilliseconds(mt.ElapsedMilliseconds);//this is from other methods
The picture is seen as it is stored now:
But I would like it to be stored like this, and the other attempts were sorted by the value of time:
Each time the application is completed, the user's new time should be sorted by how fast it was.
I will be happy for any advice
Thank you
I'm sure there are much smarter ways of achieving the same result but a very simple way would be to:
Read the contents of the file
Assign the contents to a list
Append your value to the list
Use linq to order the list accordingly
Write the list back to the file
Example:
using System.Collections.Generic;
using System.IO;
using System.Linq;
private void WriteText(string strPath, TimeSpan tsDuration)
{
//Create new list of type 'TimeSpan'
var list = new List<TimeSpan>();
//Read the contents of the file and assign to list
string line;
using (var sr = new StreamReader(strPath))
{
while ((line = sr.ReadLine()) != null)
{
list.Add(TimeSpan.Parse(line));
}
}
//Add your time value to the list
list.Add(tsDuration);
//Order the list in descending order - use OrderBy for ascending
list.OrderByDescending(i => i);
//Write contents back to file - note: append = false
using StreamWriter str = new StreamWriter(strPath, false);
foreach (var item in list)
{
str.WriteLine(item);
}
}
readonly string path = #"C:\Users\info\Desktop\žebříček.txt";
TimeSpan duration = TimeSpan.FromMilliseconds(mt.ElapsedMilliseconds);//this is from other methods
Within outlook I have various DocumentItems in folders such as the inbox and I am trying to save these to the file system within a drag/drop event.
Here is the code:
for (var i = 1; i <= _application.ActiveExplorer().Selection.Count; i++)
{
var temp = _application.ActiveExplorer().Selection[i];
var documentItem = (temp as DocumentItem);
if (documentItem == null)
continue;
var tempFileName = Path.GetTempPath() + documentItem.Subject;
documentItem.SaveAs(tempFileName);
}
They seem to save successfully and have file sizes:
But when I try to open any of them they all say they cannot be opened so they are corrupted somehow, does anyone have any ideas?
You are calling SaveAs without specifying the format, and Outlook Object Model defaults it to olMsg. You end up with an MSG file with a JPG extension.
What you need to do is loop though all attachments in the DocumentItem.Attachments collection and call Attachment.SaveAsFile. You might also want to use the Attachmeent.FileName property.
Just a general comment - multiple dot notation is evil, especially in a loo:
Selection selection = _application.ActiveExplorer().Selection;
for (var i = 1; i <= selection.Count; i++)
{
var temp = selection[i];
var documentItem = (temp as DocumentItem);
...
I wonder if there is any way to give a name to a writeableBitmap
I have 3 images in my folder. To display them in a gridView I convert them to a wrtieableBitmap
if (file.Count > 0)
{
var images = new List<WriteableBitmap>();
foreach (StorageFile f in file)
{
var name = f.Name;
Debug.WriteLine(f.Name);
ImageProperties properties = await f.Properties.GetImagePropertiesAsync();
if (properties.Width > 0)
{
var bitmap = new WriteableBitmap((int)properties.Width, (int)properties.Height);
Debug.WriteLine(bitmap.PixelWidth);
using (var stream = await f.OpenAsync(FileAccessMode.ReadWrite))
{
bitmap.SetSource(stream);
}
images.Add(bitmap);
Debug.WriteLine(images.Count);
}
}
The I add them to the gridView by binding the Data as in AlGridView.DataContext = images;
Now, once I interact with those images and I press a button I need to save the selection...but using an array with numbers [1,2,3]
Before converting them, as you may see above, I had a name (which is 1.png, 2.png, etc) but once they are writeablebitmaps I haven´t found any way to give / get a name out of them
And
bitmap.SetVale(Name, f.Name);
Does not work, since it says it cannot convert a string into a DependencyProperty
Any ideas?
Ha!. I think I have found the solution:
properties.Title = name;
var bitmap = new WriteableBitmap((int)properties.Width, (int)properties.Height);
bitmap.SetValue(NameProperty, (string)properties.Title);
Thank you all!
As per question Remove unused image objects
I was told I'd effectively have to parse a PDF file, take note of the global object names, then remove those not in use.
I would not have even an inkling of where to start.
I was having a look in VS2010 local viewer and I could see in a page there was an array called Matrix. This seems to contain the XObjects in use in the page. But Matrix does not seem to be a property that the API allows.
I also found in my reader an xrefObj array, which seems to be every object. WHen looking at the XObjects i found a number of PRStream objects which corresponded in size to teh actual images.
iTextSharp.text.pdf.PdfDictionary dictionary = reader.GetPageN(i);
iTextSharp.text.pdf.PdfImportedPage page = pdfCpy.GetImportedPage(reader, i);
iTextSharp.text.pdf.PdfDictionary res = (iTextSharp.text.pdf.PdfDictionary)iTextSharp.text.pdf.PdfReader.GetPdfObject(dictionary.Get(iTextSharp.text.pdf.PdfName.RESOURCES));
iTextSharp.text.pdf.PdfDictionary xobj = (iTextSharp.text.pdf.PdfDictionary)iTextSharp.text.pdf.PdfReader.GetPdfObject(res.Get(iTextSharp.text.pdf.PdfName.XOBJECT));
foreach (iTextSharp.text.pdf.PdfName name in xobj.Keys)
{
iTextSharp.text.pdf.PdfObject obj = xobj.Get(name);
if (obj.IsIndirect())
{
iTextSharp.text.pdf.PdfDictionary tg = (iTextSharp.text.pdf.PdfDictionary)iTextSharp.text.pdf.PdfReader.GetPdfObject(obj);
iTextSharp.text.pdf.PdfName type = (iTextSharp.text.pdf.PdfName)iTextSharp.text.pdf.PdfReader.GetPdfObject(tg.Get(iTextSharp.text.pdf.PdfName.SUBTYPE));
if (iTextSharp.text.pdf.PdfName.IMAGE.Equals(type))
{
int XrefIndex = Convert.ToInt32(((iTextSharp.text.pdf.PRIndirectReference)obj).Number.ToString(System.Globalization.CultureInfo.InvariantCulture));
iTextSharp.text.pdf.PdfObject pdfObj = reader.GetPdfObject(XrefIndex);
iTextSharp.text.pdf.PdfStream pdfStream = (iTextSharp.text.pdf.PdfStream)pdfObj;
}
}
}
This block, seems to give me the entire catalog of resources - as opposed to the specific used ones on the page.
So I guess what I'm asking for is:
-How can I match what is actually in my PDF file (I assume I make a list of all the ObjNum) references on each actual page of my File against the master list that is held in the reader.
-Remove all references that are not kept in my references list and save in place (this is a temporary file so in place would be fine.
Thanks in advance.
So to identify the images on the page, I used a PdfReaderContentParser.
iTextSharp.text.pdf.parser.PdfReaderContentParser parser = new iTextSharp.text.pdf.parser.PdfReaderContentParser(reader);
MyImageRenderListener listener = new MyImageRenderListener();
while (i < numberofPages)
{
i++;
parser.ProcessContent(i, listener);
}
The MyImageRenderListener is a new class inheriting: iTextSharp.text.pdf.parser.IRenderListener
All I did was add all the names to a list that's accessible from the original class:"
public void RenderImage(iTextSharp.text.pdf.parser.ImageRenderInfo renderInfo)
{
iTextSharp.text.pdf.parser.PdfImageObject image = renderInfo.GetImage();
if (image == null) return;
ImageNames.Add(renderInfo.GetRef().Number);
}
The I used the code posted in the original question as the basis of the master image list.
I am using the NPOI library within a c# console application to write some information to excel. The problem i seem to be facing is that the code will not run from within a a for loop (if i take the same code out of the for loop, it works and updates the spreadsheet).
I have stepped through the code, so i know it hits the createRow, SetCellValue etc in the loop, so i know its not an issue with the loop, just doesnt seem to want to do it in the loop. funny thing is i have almost identical code (VB.Net not C#) which works?
Sample C# Code
using (var fs = new FileStream(templateName, FileMode.Open, FileAccess.Read))
{
var template = new HSSFWorkbook(fs, true);
var sheet = template.GetSheet(sheetName);
int rowCount = 1;
int colCount = 0;
colCount = 0;
NPOI.SS.UserModel.Row rowIn = null;
NPOI.SS.UserModel.Cell cellIn = null;
//foreach (var item in items)
for(int i = 0; i < items.Count; i++)
{
rowIn = sheet.CreateRow(i);
cellIn = rowIn.CreateCell(1);
cellIn.SetCellValue("A");
}
//....rest of the code to save the file
}
The following VB.Net code works, it is different from the c# code where i have tried to change it to see if it would work, but previously the two was just a standard port.
Using fs As New FileStream(Server.MapPath("/exporttemplates/tender-export-template.xls"), FileMode.Open, FileAccess.Read)
Dim templateWorkbook As New HSSFWorkbook(fs, True)
Dim sheet As HSSFSheet = templateWorkbook.GetSheet("Products")
Dim productsBids As List(Of TenderProductBid) = TenderProductBid.GetTenderProductBids(tbid.id)
Dim i As Integer = 1
For Each pb As TenderProductBid In productsBids
Dim rowIn As NPOI.SS.UserModel.Row = sheet.CreateRow(i)
rowIn.CreateCell(0).SetCellValue(pb.product.productName)
rowIn.CreateCell(1).SetCellValue(pb.quantity)
rowIn.CreateCell(2).SetCellValue(pb.bid)
i += 1
Next
'....rest of the code to save the file
End Using
The documentation is not clear so it is not plain to see what exactly is wrong with the code for it work outside of a loop but no in it, when the loop is working and the information is being passed?
Seems there is an issue with my loop somewhere, not sure where as the breakpoints are being hit and the values are there, the library doesn't seem to like foreach for some reason.
I'm not sure what your doing in the code above, your using a variable called "items" in the c# example but I don't see it declared anywhere.
Here is an basic example on how to read a XLS file with a worksheet called "sheet1", full of values in column A.
FileStream fs = new FileStream(templateName, FileMode.Open, FileAccess.Read);
HSSFWorkbook workbook = new HSSFWorkbook(fs);
Sheet sheet1 = workbook.GetSheet("Sheet1");
List<string> items = new List<string>();
for (int i = 1; i <= sheet1.LastRowNum; i++)
{
string item = sheet1.GetRow(i).GetCell(0).ToString();
items.Add(item);
}
Make sure you properly dispose of your stream (Using or TryCatchFinally).
Try to change the FileAccess mode to "FileAccess.ReadWrite"