How to make list view with Images with swipe? - c#

I have an array of paths to images and I need to show this images. I see that the best way is to use smth like ListView and set as a source my images...
I found such solution https://stackoverflow.com/a/17382282/5709159
public DetailImageTraceForm()
{
InitializeComponent();
Text = FormTitle;
List<string> adress = new List<string>()
{
"http://i.telegraph.co.uk/multimedia/archive/02351/Jaguar-F-type-9_2351861k.jpg",
"http://i.telegraph.co.uk/multimedia/archive/02351/Jaguar-F-type-5_2351885k.jpg",
"http://i.telegraph.co.uk/multimedia/archive/02351/Jaguar-F-type-7_2351893k.jpg",
};
ImageList il = new ImageList();
DownloadImagesFromWeb(adress, il);
il.ImageSize = new Size(92, 92);
int count = 0;
Lv_images.LargeImageList = il;
List<string> names = new List<string>() { "1", "2", "3", "4" };
foreach (string s in names)
{
ListViewItem lst = new ListViewItem();
lst.Text = s;
lst.ImageIndex = count++;
Lv_images.Items.Add(lst);
}
}
private void DownloadImagesFromWeb(List<string> adress, ImageList il)
{
foreach (string img in adress)
{
System.Net.WebRequest request = System.Net.WebRequest.Create(img);
System.Net.WebResponse resp = request.GetResponse();
System.IO.Stream respStream = resp.GetResponseStream();
Bitmap bmp = new Bitmap(respStream);
respStream.Dispose();
il.Images.Add(bmp);
}
}
But I get such result
But actually what I need is : each image need to be alone and take the full screen(container) and something like arrows on sides like left(priv) right(next) and when user click next arrow, so next image swipe and replace current image.
Is there such out of the box solution in winforms or not?
I know that I can put ImageBox at my container and two buttons, but I hope that there is ListView solution because I would like to have kind of swipe animation between images...

Related

Expander Control - Read txt-Files in C# (WindowsForms)

I‘m trying to add the second .txt file (book2) into Text Expander control, unfortunately without success.
public Form1()
{
InitializeComponent();
CreateAccordion();
}
private void CreateAccordion(string book1)
{
Accordion accordion = new Accordion();
accordion.Size = new Size(400, 350);
accordion.Left = 10;
Expander expander1 = new Expander();
expander1.BorderStyle = BorderStyle.FixedSingle;
ExpanderHelper.CreateLabelHeader(expander1, "Book1", SystemColors.ActiveBorder);
CreateContentLabel(expander1, book1, 140);
accordion.Add(expander1);
Expander expander2 = new Expander();
expander2.BorderStyle = BorderStyle.FixedSingle;
ExpanderHelper.CreateLabelHeader(expander2, "Book2", SystemColors.ActiveBorder);
CreateContentLabel(expander2, "book2", 120);
accordion.Add(expander2);
this.Controls.Add(accordion);
}
The following code reads only a single txt.file. Can anyone help me please?
private void CreateAccordion()
{
ReadTxtFiles();
}
private void ReadTxtFiles()
{
string path = #"C:\..\..\READBOOKS";
string[] files = new string[] { "book1.txt", "book2.txt" };
foreach (string file in files)
{
string fullPath = Path.Combine(path, file);
string booktxt = File.ReadAllText(fullPath);
string book1 = booktxt;
CreateAccordion(book1);
}
}
I‘m trying to add the second .txt file (book2) into Text Expander control, unfortunately without success.
The main reason behind this is because you're only passing the string from your first book, you'll need to pass all of the text.
private void ReadTxtFiles()
{
string path = #"C:\..\..\READBOOKS";
string[] files = new string[] { "book1.txt", "book2.txt" };
List<string> books = new List<string>();
foreach (string file in files)
{
string fullPath = Path.Combine(path, file);
string booktxt = File.ReadAllText(fullPath);
books.Add(booktxt);
}
CreateAccordion(books);
}
Change the signature of CreateAccordion:
private void CreateAccordion(List<string) books)
{
Accordion accordion = new Accordion();
accordion.Size = new Size(400, 350);
accordion.Left = 10;
Expander expander1 = new Expander();
expander1.BorderStyle = BorderStyle.FixedSingle;
ExpanderHelper.CreateLabelHeader(expander1, "Book1", SystemColors.ActiveBorder);
CreateContentLabel(expander1, books[0], 140);
accordion.Add(expander1);
Expander expander2 = new Expander();
expander2.BorderStyle = BorderStyle.FixedSingle;
ExpanderHelper.CreateLabelHeader(expander2, "Book2", SystemColors.ActiveBorder);
CreateContentLabel(expander2, books[1], 120);
accordion.Add(expander2);
this.Controls.Add(accordion);
}
Please note that I'm accessing index's here, you may want to check that the index exist before trying to access it. Also there's more way's than this, but should give you a better understanding to accomplish this.

Retreive stackpanel children from GridViewItem (UWP)

I'm wondering how I can get the contents of a stackpanel which has been added to a GridViewItem (YouTube data API, the stackpanel contains a bitmap image for a thumbnail, the title of the video and a hidden TextBlock with the video ID for reference).
To clarify, I need to be able to obtain the contents of the StackPanel that is contained in the GridViewItem (such as the string of that hidden TextBox) so I can use the data to display the correct video.
Here's the code I use to create the stackpanel.
public async Task SearchByKeyword(string searchTerm)
{
GeneralFunctions gf = new GeneralFunctions();
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = gf.YouTubeAPIKey,
ApplicationName = this.GetType().ToString()
});
var searchListRequest = youtubeService.Search.List("snippet");
searchListRequest.Q = searchTerm; // Replace with your search term.
searchListRequest.MaxResults = 50;
searchListRequest.SafeSearch = SearchResource.ListRequest.SafeSearchEnum.Strict;
// Call the search.list method to retrieve results matching the specified query term.
var searchListResponse = await searchListRequest.ExecuteAsync();
List<string> videos = new List<string>();
List<string> channels = new List<string>();
List<string> playlists = new List<string>();
// Add each result to the appropriate list, and then display the lists of
// matching videos, channels, and playlists.
foreach (var searchResult in searchListResponse.Items)
{
switch (searchResult.Id.Kind)
{
case "youtube#video":
// Create a new StackPanel to insert as a ListViewItem
StackPanel sPanel = new StackPanel();
sPanel.HorizontalAlignment = HorizontalAlignment.Stretch;
sPanel.VerticalAlignment = VerticalAlignment.Stretch;
sPanel.Orientation = Orientation.Vertical;
sPanel.Width = 160;
sPanel.Height = 160;
// Create new StackPanel "Child" elements with alignment and width
TextBlock tb1 = new TextBlock();
tb1.Text = searchResult.Snippet.Title;
tb1.TextWrapping = TextWrapping.WrapWholeWords;
tb1.Width = 160;
// Create new StackPanel "Child" elements with alignment and width
TextBlock tb2 = new TextBlock();
tb2.Text = searchResult.Snippet.ChannelId;
tb2.TextWrapping = TextWrapping.WrapWholeWords;
tb2.Width = 160;
// Create new StackPanel child element for a 120x120 thumbnail of the videos from the search results
Image image = new Image();
image.Source = new BitmapImage(new Uri(searchResult.Snippet.Thumbnails.Default__.Url, UriKind.Absolute));
// Add a "hidden" child element to each stackpanel to hold the video identity
TextBlock h1 = new TextBlock();
h1.Text = searchResult.Id.VideoId.ToString();
h1.Visibility = Visibility.Collapsed;
h1.TextWrapping = TextWrapping.WrapWholeWords;
sPanel.Children.Add(image);
sPanel.Children.Add(tb1);
sPanel.Children.Add(tb2);
sPanel.Children.Add(h1);
SearchResults.Items.Add(sPanel);
break;
case "youtube#channel":
//SearchResults.Items.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId));
break;
case "youtube#playlist":
//playlists.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId));
break;
}
}
//Console.WriteLine(String.Format("Videos:\n{0}\n", string.Join("\n", videos)));
//Console.WriteLine(String.Format("Channels:\n{0}\n", string.Join("\n", channels)));
//Console.WriteLine(String.Format("Playlists:\n{0}\n", string.Join("\n", playlists)));
}
Any help is appreciated.
Thanks
I guess I should never code tired right? The answer to my own question is actually very very simple
if (SearchResults.SelectedItems.Count > 0)
{
StackPanel sp = (StackPanel)SearchResults.SelectedItem;
TextBlock tb1 = (TextBlock)sp.Children[1];
TextBlock tb2 = (TextBlock)sp.Children[2];
TextBlock hf1 = (TextBlock)sp.Children[3];
}

uwp InkCanvas save stokes as svg

I am trying to save InkCanvas , InkStorkes as a SVG. I found a previous question that works for Wpf,but I have been unable to get it to work with uwp. Wpf InkCanvas save stokes as svg
. I made some changes to it, but I am running into issues with GetGrometry and XamlWriter.
var svg = new SvgDocument();
var colorServer = new SvgColourServer(System.Drawing.Color.Black);
var group = new SvgGroup { Fill = colorServer, Stroke = colorServer };
svg.Children.Add(group);
foreach (var stroke in inkCanvas.InkPresenter.StrokeContainer.GetStrokes())
{
var geometry = stroke.GetGeometry(stroke.DrawingAttributes).GetOutlinedPath‌​Geometry();
var s = XamlWriter.Save(geometry);
if (s.IsNotNullOrEmpty())
{
var element = XElement.Parse(s);
var data = element.Attribute("Figures")?.Value;
if (data.IsNotNullOrEmpty())
{
group.Children.Add(new SvgPath
{
PathData = SvgPathBuilder.Parse(data),
Fill = colorServer,
Stroke = colorServer
});
}
}
}
I implemented an approach from the comments Xavier Xie - MSFT.
The main idea was to use own implementation of ICanvasPathReceiver interface - here is CanvasGeometryToSvgPathReader class:
var svgDocument = new CanvasSvgDocument(canvasDevice);
foreach (var stroke in InkCanvas.InkPresenter.StrokeContainer.GetStrokes())
{
var canvasGeometry = CanvasGeometry.CreateInk(drawingSession, new[] {stroke});
var pathReceiver = new CanvasGeometryToSvgPathReader();
canvasGeometry.SendPathTo(pathReceiver);
var element = svgDocument.Root.CreateAndAppendNamedChildElement("path");
element.SetStringAttribute("d", pathReceiver.Path);
element.SetColorAttribute("fill", stroke.DrawingAttributes.Color);
}
As result ballpoint pen renders fine, highlighter worse and pencil doesn't render.
Full source code: https://github.com/ycherkes/InkToSvg

C# add an image (instead of text) to ListViewItem

I'm dynamically updating a ListView like that:
ListViewItem item = new ListViewItem();
item.Text = "Text1";
item.SubItems.Add("Text2");
item.SubItems.Add("Text3");
item.SubItems.Add("Text4");
item.Tag = i;
listView.Items.Add(item);
now I want that instead of Text4 will be a lil icon I will get dynamically from a url. I read a lot of threads and tried many things - but I can't get this to work..
You need to implement ImageList in your function.
Code :
// get picture resource
WebClient _web = new WebClient();
byte[] _data = _wb.DownloadData("http://www.myzony.com/usr/uploads/2017/03/3197402477.png");
MemoryStream _ms = new MemoryStream(_data);
// Loaded to imagelist
ImageList list = new ImageList();
list.Images.Add("pic1",Image.FromStream(_ms));
// bind listview
listView1.SmallImageList = list;
ListViewItem _item1 = new ListViewItem();
_item1.Text = "Test";
_item1.SubItems.Add("Test2");
_item1.SubItems.Add("pic1");
_item1.ImageKey = "pic1";
listView1.Items.Add(_item1);
Effect Image:

Windows Phone 8 C# - Change the name of an xaml item in a foreach loop

I have a small problem here and I don't know how to fix it myself.
What i'm trying to do, is i want to change the image source of every item in the array (names).
This is what my code looks like now:
(the m at m.Source is where the name should go)
private void Stars()
{
string[] LevelPick = new string[] {"Stage1Level1", "Stage1Level2", "Stage3Level3" };
foreach (string m in LevelPick)
{
string Stars = appSettings[""+m+""].ToString();
if(Stars == "0")
{
BitmapImage bm = new BitmapImage(new Uri("/Resources/Images/GeenSter.png", UriKind.RelativeOrAbsolute));
m.Source = bm; // error here
}
else
{
BitmapImage bm = new BitmapImage(new Uri("/Resources/Images/GeenSter.png", UriKind.RelativeOrAbsolute));
m.Source = bm; // same here
}
}
}
The error that i get is:
'string' does not contain a definition of 'Source'.
Does anybody know how to fix this?
Thanks in advance!
You could simply have an array of your Image controls, instead of an array of their names:
var images = new Image[] { Stage1Level1, Stage1Level2, Stage3Level3 };
Now you could iterate over these images:
foreach (var image in images)
{
var stars = appSettings[image.Name].ToString();
if (stars == "0")
{
image.Source = new BitmapImage(new Uri(...));
}
...
}

Categories

Resources