|
-I am trying to learn how to load up a Gallery of images to asp website.I understand I could load thumbnail images to reduce original size. Here is some code I tried after googling, but its still loading original size.
-Also, Can I make these images look more alive, like clikable, or double their size whenclicked?
protected void btnsave_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
string targetPath = Server.MapPath("Images/" + filename);
Stream strm = fileupload1.PostedFile.InputStream;
var targetFile = targetPath;
GenerateThumbnails(0.5, strm, targetFile);
BindDataList();
}
private void GenerateThumbnails(double scaleFactor, Stream sourcePath,string targetPath)
{
using (var image = System.Drawing.Image.FromStream(sourcePath))
{
var newWidth = (int)(image.Width * scaleFactor);
var newHeight = (int)(image.Height * scaleFactor);
var thumbnailImg = new Bitmap(newWidth, newHeight);
var thumbGraph = Graphics.FromImage(thumbnailImg);
thumbGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbGraph.DrawImage(image, imageRectangle);
thumbnailImg.Save(targetPath, image.RawFormat);
}
}
protected void BindDataList()
{
DirectoryInfo dir = new DirectoryInfo(MapPath("~/Images"));
FileInfo[] files = dir.GetFiles();
ArrayList listItems = new ArrayList();
foreach (FileInfo info in files)
{
listItems.Add(info);
}
dtlist.DataSource = listItems;
dtlist.DataBind();
}
modified 30-Oct-11 23:34pm.
|
|
|
|
|
|
For generating thumbnails, take a look at the GetThumbnailImage method of the Image class. It's perfect for this — I've used it for just this purpose in the past (displaying small images to save bandwidth and load time on an ASP.NET site).
I can't help you with displaying them though, as you don't show any markup or your Page_Load method and the like.
Remember, though, that if you are generating thumbnails on the fly in memory on the server, these do not exist anywhere on disk. When the HTML is loaded in the client browser, the img element needs an src URL. This is, as you probably know, typically a direct link to a .jpg somewhere on your webserver virtual directory or something. Usually it is a disk file. Since these are not really saved, you can't link to a file. But you have to link to something!
What we did is create an aspx page that returns image data (not html). You can find a good guide to this here (which also shows using the GetThumbnailImage method I mentioned before). You then will basically use an Image control and set its ImageUrl to MakeThumbnail.aspx?file=picture.jpg or the like. In our case, we had images stored in a database, so we used an integer id instead of a file name.
|
|
|
|
|
Check out ... here on CP : "LeftImage - An Image Optimization Library with Fluent Interface"[^].
"Last year I went fishing with Salvador Dali. He was using a dotted
line. He caught every other fish." Steven Wright
|
|
|
|
|
Another route to consider is to use the newer features of CSS Image transformations (which I have not tried myself)?
A recent SitePoint article; although its focus is on background-image transformation, the article implies a broader usage: "Scaling, skewing and rotating any element is possible with the CSS3 transform property. It’s supported in all modern browsers (with vendor prefixes) and degrades gracefully."[^]
good luck, Bill
"Last year I went fishing with Salvador Dali. He was using a dotted
line. He caught every other fish." Steven Wright
|
|
|
|
|
hi, I'm using the below code to close some programs.But it seems like it's not the best practice. would you recommend me anything better? Thanks.
Process[] runningProcesses;
Queue programsToBeClosed;
public void closePrograms(Queue programsToBeClosed)
{
foreach (string program in programsToBeClosed)
{
foreach (Process item in runningProcesses)
{
try
{
if (item.ProcessName == program || item.MainWindowTitle == program)
{
item.Kill();
}
}
catch (Exception)
{
}
}
}
}
|
|
|
|
|
You declare your variables runningProcesses and programsToBeClosed but you never initialize them.
So they will be null in your foreach loops.
Moreover, are you sure a Queue is a list of strings ? Did you try to debug your piece of code to see what's going on exactly ?
No memory stick has been harmed during establishment of this signature.
|
|
|
|
|
my variables are already initialized and code is already working and doing its job, I just wanted to ask whether it's good practice.Because nested foreach seemed to much code to me. Thanks
|
|
|
|
|
I would avoid the use of one of the foreach loops. This way :
Process[] runningProcesses;
List<string> programsToBeClosed;
public void closePrograms(List<string> programsToBeClosed)
{
foreach (Process item in runningProcesses)
{
if ((programsToBeClosed.Contains(item.ProcessName)) || (programsToBeClosed.Contains(item.MainWindowTitle)))
{
try
{
item.Kill();
}
catch (Exception)
{
}
}
}
}
Hope this helps.
No memory stick has been harmed during establishment of this signature.
|
|
|
|
|
this is it! Thank you very much for your help
|
|
|
|
|
This might actually be worse as Contains probably iterates over the collection and you're calling it twice.
|
|
|
|
|
This might, even if I didn't test it. Thanks for pointing it out.
Maybe using two HashSet<string> would help ; first, lookups for hash should be quicker ; second, it could be taken profit of Union() and Intersect() methods to build a list of process names to be closed.
But it's just a guess ; and I won't have time to measure gains or losses. Maybe the OP will.
No memory stick has been harmed during establishment of this signature.
|
|
|
|
|
It depends on what the loops are doing. In this case it's unnecessary and phil.o's response is good. But it may still perform similar to what you presented because the Contains may simply do what your inner foreach is doing. I also assume that the lists will be fairly short so it won't matter too much.
If you have long lists, and they are sorted (or can be), then I recommend a different tack, based on Ye Olde Master File Update*...
0) While both lists have items to process:
0.1) Compare the two current items
0.1.1) If they match, process the item, and advance both lists
0.1.2) If the first list's item is less than the second list's item, advance the first list
0.1.3) Otherwise, advance the second list
1) Do stuff with any items left in the first list (you won't need this)
2) Do stuff with any items left in the second list (you won't need this)
This way you only iterate each list at most once.
* The only thing I learned on COBOL class , but I have needed it many times in my career, including within the last month.
|
|
|
|
|
thanks for the detailed explanation.I solved the problem already but you gave me an idea about future problems. Regards...
|
|
|
|
|
PIEBALDconsult wrote: 0.1.1) If they match, process the item, and advance both lists
0.1.2) If the first list's item is less than the second list's item, advance the first list 0.1.1 assumes a sorting (which you state is required) ... of the two lists ... but also assumes matching of items in each list so that you can be assured for each ith. entry in List 1, the ith. entry in List2 is what you need to compare. If List2, or List1 has 'intermediate,' or 'extra' values, this technique will clearly fail.
0.1.2 implies that the lists' items have some possible form of quantitative comparison possible (in this case using less-than) : this is not relevant to the scenario implied in this thread by the OP.
1, and 2. imply a possible difference in the length of the lists, which you say can be ignored here: but what happens if in the the part of list 2 (which is longer than list1) is an item that matches an item in list1 which you did not find a match for as you progressed sequentially ?
I'm not commenting on this from a 'picky-picky' perspective: I am generally curious as to why you think this 'schema' is broadly useful, or relevant to the OP's scenario, and I'd be even more curious as to how you actually used it recently.
Now that we have Linq, selecting items that match from Lists is a whole lot easier (if more difficult to master ... at least for me).
best, Bill
"Last year I went fishing with Salvador Dali. He was using a dotted
line. He caught every other fish." Steven Wright
|
|
|
|
|
BillWoodruff wrote: assumes a sorting
Yes, both lists must be sorted.
BillWoodruff wrote: for each ith. entry in List 1, the ith. entry in List2 is what you need to
compare
No, when there is no match, only the list with the lower value is advanced.
BillWoodruff wrote: how you actually used it recently.
Given lists of database items (creation scripts of tables, views, procedures, etc.) between dev and prod (for instance), find those which exist in only one environment or whose scripts are not identical. (I may write an article on it.)
See also: http://cs.uni.edu/~east/teaching/cobol/topics/seq_update/algorithms.html[^]
|
|
|
|
|
I agree.
Now if you have an elegant implementation, that would make an interesting Tip/Trick IMO.
BTW: IMO what Bill hinted at is correct, the lists need to be fully sorted; e.g. sorting people on last name only would not guarantee a unique sorting order and then your approach would fail.
modified 31-Oct-11 13:29pm.
|
|
|
|
|
I've been giving that some thought, but, while a generic solution may be devised, I'm not sure how elegant it would be because of the variety of possible data sources (my latest usage uses DataReader s).
I can imagine a MasterFileUpdate<T,U> that requires not only references to two data sources (which may not be the same type), but also a delegate to a Comparer<T,U>, and delegates for advancing the sources. Plus, as with DataReader s (but not with List s), some data sources will need an initial advance before they can be read.
My concept then, once the Updater is set up and put in motion, would involve the Updater raising events to report its findings as it goes. The calling code would only attach handlers for the events it needs.
... on further thought I think there would need to be four generic parameters, not two .
It's still an interesting exercise and perhaps I'll get right on it.
Edit: I have it roughed in, but not tested. It may not be too bad after all.
Edit: Tested it with List<int> s -- it seems to work. I need to refactor it into a recent app and test it further.
modified 2-Nov-11 21:35pm.
|
|
|
|
|
This response is an "aside:" it is not an answer to the OP's question, but a comment on the scenario, and a corollary question the question and answers raised ... for me.
Looks like you got what you need from the answers here, and, after all, your original code works.
But, I am surprised that this code did not run into the issue of a collection which is self-modified during a foreach, or for, loop, throwing a compile error.
A problem which I've worked around in the past by using Linq to cast an existing collection to a List<whatever>, and then enumerating that generated collection which can be modified/deleted/whatever without causing an error, a trick I learned somewhere on StackOverFlow.
But, I guess the 'simpler' structure of Array, by definition, cannot have this problem.
best, Bill
"Last year I went fishing with Salvador Dali. He was using a dotted
line. He caught every other fish." Steven Wright
modified 31-Oct-11 1:20am.
|
|
|
|
|
both the array and the queue contain snapshots, i.e. they are views on the recent past; they don't get affected by changing the present, so there is no issue using them in a foreach.
|
|
|
|
|
BillWoodruff wrote: But, I am surprised that this code did not run into the issue of a collection
which is self-modified during a foreach, or for, loop, throwing a compile
error.
There's no reason it would. The OP is not actually attempting to modify the list, rather he is acting on an item of the list which represents a snapshot. If he attempted to remove the item from the list, he would hit exactly the issue you describe.
|
|
|
|
|
The area of an MDI container window is covered by an MdiClient control; consequently, Form.BackColor and Form.BackgroundImage are ineffective. So How to set the background for this form.
|
|
|
|
|
I would think you need to do P/Invoke for that...
Philippe Mori
|
|
|
|
|
You could, but it's worthess and will slow down your applications rendering speed.
Again, you can't do it as design time. You have to do it at runtime.
Again, you have to enumerate through the MdiParent Controls collection. Once you find the MdiClient control, you can set its BackgroundImage property to whatever you want.
|
|
|
|
|
+5 Right on target, Dave, and you reminded me of when ... several years ago ... I did some stuff with MDI (yuck), and I went back today, and took a look at the MdiClient component that is created when you set a Form's IsMdiContainer Property = true.
The WinForms designers, probably having a bad hair day, set the Text and Name properties of MdiClient to an empty string, which means you can't do something like this to find it:
private MdiClient theMDIClientControl;
Control[] potentialMDIClientControls = this.Controls.Find("MdiClient", false);
if (potentialMDIClientControls.Length > 0) theMDIClientControl = potentialMDIClientControls[0] as MdiClient; So, as you said, you gotta iterate/enumerate over the Controls collection of the Form.
"Last year I went fishing with Salvador Dali. He was using a dotted
line. He caught every other fish." Steven Wright
|
|
|
|
|