How to put into Array some source to WPF Controls called Image? O this forum i find, but how to make array?
BitmapImage logo = new BitmapImage()
logo.BeginInit();
logo.UriSource = new Uri("pack://application:,,,/img/3.jpg");
logo.EndInit();
tmpimage.Source = logo;
But i need sometring like this:
Image[] img = new Image[3];
img[0].Source = new Uri("pack://application:,,,/img/3.jpg");
tmpimage.Source = img[0];
Image[] images = new Image[3] { new Image(), new Image(), new Image() };
images[0].Source = new BitmapImage(new Uri("pack://application:,,,/img/3.jpg"));
images[1].Source = new BitmapImage... // etc...
Alternatively, make your image factory a function and use LINQ:
Image CreateBitmap(string uri)
{
return new Image() { Source = new BitmapImage(new Uri(uri)) };
}
Image[] GetImages()
{
var imageUris = new[]
{
"pack://application:,,,/img/3.jpg",
"pack://application:,,,/img/elephant.jpg",
"pack://application:,,,/img/banana.jpg"
};
return imageUris.Select(CreateBitmap).ToArray();
}
Dynamic Array of BitmapImage :
BitmapImage[] iHab;
BitmapImage CreateBitmap(string uri)
{
return new BitmapImage(new Uri(uri));
}
BitmapImage[] GetImages()
{
string currDir = Directory.GetCurrentDirectory();
string[] imageUris;
//Get directory path of myData
string temp = currDir + "\\Media\\hcia\\";
imageUris = Directory.GetFiles(temp, "habitation*.png");
return imageUris.Select(CreateBitmap).ToArray();
}
private void Rec_hab_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
iHab = GetImages();
pointer.Source = iHab[7]; // the 7th image : can be manipulated with an i++
}
Related
I need to load some pictures into a ListView. I've figured out how to do this with images stored on my local drive: when run, the code below displays exactly what I need.
private void getLocalImages()
{
List<string> imageList = new List<string>();
imageList.Add("C:\\....\\Pic1.png");
imageList.Add("C:\\....\\Pic2.png");
imageList.Add("C:\\....\\Pic3.png");
ImageList imgl = new ImageList();
imgl.ImageSize = new Size(200, 300);
imgl.ColorDepth = ColorDepth.Depth32Bit;
for (int i = 0; i < imageList.Count; i++)
{
imgl.Images.Add(Image.FromFile(imageList[i]));
listView1.Items.Add("", i);
}
listView1.LargeImageList = imgl;
}
How do I now modify this to load pictures stored on the web instead?
Edit: I'm using .NET Core 6.0, and this is a Windows Form application
I figured it out:
private void getWebImages()
{
HttpClient client = new HttpClient();
List<string> imageList = new List<string>();
imageList.Add("https://www. ... .com/.../Pic1.jpg");
imageList.Add("https://www. ... .com/.../Pic1.jpg");
imageList.Add("https://www. ... .com/.../Pic1.jpg");
ImageList imgl = new ImageList();
imgl.ImageSize = new Size(300, 200);
imgl.ColorDepth = ColorDepth.Depth32Bit;
for (int i = 0; i < imageList.Count; i++)
{
Stream r = client.GetStreamAsync(imageList[i]).Result;
imgl.Images.Add(Image.FromStream(r));
listView1.Items.Add("", i);
}
listView1.LargeImageList = imgl;
}
I have list of images and want to assign them to image attribute to be displayed in a datatable. How can I do that?
The idea is I want to retrieve value from database and depend on that value I want to display images. for example; if the value=2, then display 2 images; if it=3, then display 3 images...
I got this: can not implicitly convert type
System.Collections.Generic.List to System.Windows.Controls.Image for this statement: Project.team_members = HandCards;
This is my code:
Project class
public class Projects
{
public string developer_name { get; set; }
public Image team_members { get; set; }
}
asmx
List<string> developerNames = new List<string>();
List<Image> HandCards = new List<Image>();
Image image = new Image();
BitmapImage source = new BitmapImage();
source.BeginInit();
source.UriSource = new Uri("images/img.jpg", UriKind.Relative);
source.EndInit();
image.Source = source;
while (somthing)
{
developerNames.Add((string)rdrDev["user_name"]);//value returned from DB
HandCards.Add(image);
}
Project.developer_name = string.Join(", ", developerNames);//I want to do
//something like this for the images. this statement put all developers
//together seperated by ','; I want to do the same to the next statement but
//don't know how to do it for image type
Project.team_members = HandCards;
aspx
var datatableVariable = $('#projects-progress').DataTable({
data: data,
columns: [
{ 'data': 'developer_name' },
{ 'data': 'team_member' },
]
I haven't worked with asp in a while but for the first part of your question, you could follow a similar implementation as the one shown below:
In HTML Markup/ASPX Page:
Say you have three images viz.
<img src="~/Img1.jpg" runat="server" />
<img src="~/Img2.jpg" runat="server" />
<img src="~/Img3.jpg" runat="server" />
In C# Code Behind:
string filePath1 = "Img1.jpg"; //You'll do the same for the other two images
Image img1 = new Image(string filePath1);
Image img2 = new Image(string filePath2);
Image img3 = new Image(string filePath3);
List<Image> listOfImageCollection = new List<Image>();
listOfImageCollection.Add(img1);
listOfImageCollection.Add(img2);
listOfImageCollection.Add(img3);
DataTable dt = new DataTable();
foreach(Image imgs in listOfImageCollection)
{
dt.DataSource = imgs;
dt.DataBind();
}
string myConnectionString = "";
SqlConnection mySQLConnection;
SqlCommand mySQLCommand;
SqlDataReader mySQLDataReader;
myConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStringToTheDB"].ConnectionString;
using (mySQLConnection = new SqlConnection(myConnectionString))
{
mySQLCommand = new SqlCommand("storedProcedureNameToGetValue", mySQLConnection);
mySQLCommand.CommandType = CommandType.StoredProcedure;
mySqlParameter = new SqlParameter();
mySqlParameter.ParameterName = "#Value";
mySqlParameter.Value = Value;
mySQLCommand.Parameters.Add(mySqlParameter);
mySQLConnection.Open();
mySQLDataReader = mySQLCommand.ExecuteReader();
//Create a class to hold your DB properties
ClassName classInstance = new ClassName();
while (mySQLDataReader.Read())
{
classInstance.Value = Convert.ToInt32(mySQLDataReader["DBColumnNameHoldingValue"]);
}
if(classInstance.Value == 1 && dt.Count == 1)
{
//Display one Image then follow the same procedure for the other conditions
}
}
I'm using the DexExpress.Xpf.Map.v15.1 MapControl and trying to create code that will be able to run in a Windows Service (i.e. no front end) to generate maps as images.
I have the following code which creates a map, adds GeoPoints to a PolyLine and then adds that PolyLine to a map. This works fine in my WPF test harness application:
public MapWindow1()
{
InitializeComponent();
var mapControl = new MapControl
{
Name = "TheMap",
ZoomLevel = 4,
CenterPoint = new GeoPoint(47, 5)
};
var imageLayer = new ImageTilesLayer
{
DataProvider = new OpenStreetMapDataProvider()
};
mapControl.Layers.Add(imageLayer);
var vectorLayer = new VectorLayer();
mapControl.Layers.Add(vectorLayer);
var mapItemStorage = new MapItemStorage();
vectorLayer.Data = mapItemStorage;
var polyLineCollection = DbfGenerator.GeneratePolyLineCollection();
polyLineCollection.ForEach(line =>
{
var mapItem = new MapPolyline();
mapItemStorage.Items.Add(mapItem);
mapItem.Points.AddRange(line);
});
mapControl.MouseDoubleClick += mapControl_MouseDoubleClick;
MainGrid.Children.Add(mapControl);
}
This runs and adds the map control to the WPF page without a problem. I've then added double click handler to export the map as an image:
void mapControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var m = GenerateMapPng((MapControl) sender);
m.Save(#"C:\0Temp\test.bmp");
}
public Bitmap GenerateMapPng(MapControl map)
{
Bitmap bmp;
// export image from map
using (MemoryStream ms = new MemoryStream())
{
var exportOptions = new ImageExportOptions(ImageFormat.Png);
map.ExportToImage(ms, exportOptions);
ms.Position = 0;
bmp = new Bitmap(System.Drawing.Image.FromStream(ms));
}
return bmp;
}
Again - this works without a problem.
However, If I remove the UI element and run the following code:
public MapWindow1()
{
InitializeComponent();
var mapControl = new MapControl
{
Name = "TheMap",
ZoomLevel = 4,
CenterPoint = new GeoPoint(47, 5)
};
var imageLayer = new ImageTilesLayer
{
DataProvider = new OpenStreetMapDataProvider()
};
mapControl.Layers.Add(imageLayer);
var vectorLayer = new VectorLayer();
mapControl.Layers.Add(vectorLayer);
var mapItemStorage = new MapItemStorage();
vectorLayer.Data = mapItemStorage;
var polyLineCollection = DbfGenerator.GeneratePolyLineCollection();
polyLineCollection.ForEach(line =>
{
var mapItem = new MapPolyline();
mapItemStorage.Items.Add(mapItem);
mapItem.Points.AddRange(line);
});
var m = GenerateMapPng(mapControl);
m.Save(#"C:\0Temp\test.bmp");
}
I get a NullReferenceException on the "map.ExportToImage(ms, exportOptions);" line.
I'm assuming that when the WPF application loads it calls a method to initialise the map, but I can't find anything that I can manually do to the control (Load/Init method) to mimic this.
Is there a way to trick the MapControl in to thinking it is in a WPF page when it isn't?
DevExpress said that this was not possible due to the maps reliance on the UI Thread in WPF
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(...));
}
...
}
I have no idea how to put doubleclick events to multiple pushpins that are initialized in the program itself(not in the xaml). The program will get lat and long data from a text file.
here's my current code:
public partial class BingMaps : Window
{
string role;
string nick;
int countLines=0;
public BingMaps(string value,string role2)
{
InitializeComponent();
nick = value;
role = role2;
setCount();
int cLines = 0;
Pushpin[] pushpins = new Pushpin [countLines];
StreamReader z = new StreamReader("dorms.txt");
while (z.Peek() > 0)
{
var pushpinLayer = new MapLayer();
pushpinLayer.Name = "PushPinLayer";
intraMap.Children.Add(pushpinLayer);
string line = z.ReadLine();
string[] temp = line.Split(new char[] { ';' });
var location = new Location(double.Parse(temp[3]), double.Parse(temp[4]));
var pushpin = new Pushpin();
pushpin.Name = "MyNewPushpin";
pushpin.Background = new SolidColorBrush(Colors.Blue);
pushpin.Location = location;
pushpin.ToolTip = "" + temp[0];
pushpins[cLines] = pushpin;
pushpinLayer.Children.Add(pushpins[cLines]);
cLines = cLines + 1;
}
z.Close();
}
public void setCount()
{
using (StreamReader cRead = new StreamReader("dorms.txt"))
{
while (cRead.Peek() > 0) {
cRead.ReadLine();
countLines = countLines + 1;
}
}
}
}
i'm quite new to c# so please bear with me. I need to display different data for each pushpin.
you can register to MouseDoubleClick event
var pushpin = new Pushpin();
pushpin.MouseDoubleClick += (sender, ea) => {
// do something
};