using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Net;
using System.Drawing;
namespace Bella
{
class ReadDirs
{
static void Main(string[] args)
{
string pathA = @"W:\FirstChild\JeanShower\";
int LengthPathA = pathA.Length;
string editpath = @"E:\Rename\JeanShower\";
System.IO.Directory.CreateDirectory(editpath);
// These lines find all the Consoles
// Take a snapshot of the Console system.
System.IO.DirectoryInfo file1 = new System.IO.DirectoryInfo(pathA);
IEnumerable<System.IO.FileInfo> list1 = file1.GetFiles("*.*", System.IO.SearchOption.TopDirectoryOnly)
.Where(d => d.FullName.Contains(".jpg"));
System.IO.DirectoryInfo dirfile1 = new System.IO.DirectoryInfo(pathA);
IEnumerable<System.IO.DirectoryInfo> dirlist1 = dirfile1.GetDirectories("*.*", System.IO.SearchOption.TopDirectoryOnly)
.Where(d => d.FullName.Contains(".jpg"));
Console.WriteLine("Step 2 Consoles path A");
using (System.IO.StreamWriter afile1 = new System.IO.StreamWriter(@"E:\rename\afile1.txt", false))
foreach (var a in list1)
{
afile1.WriteLine(a.FullName.Substring(LengthPathA).ToString());
}
Console.WriteLine("aConsole1.txt created from list1");
int n = 0;
String[] linesA = System.IO.File.ReadAllLines(@"E:\rename\afile1.txt");
foreach (var s in linesA)
{
n++;
System.Console.WriteLine(s);
}
Console.WriteLine("N= {0}", n);
Console.WriteLine("Length of Path {0}", LengthPathA);
Console.WriteLine("Path is {0}", pathA);
string[] sourceFiles = Directory.GetFiles(pathA, "*", SearchOption.TopDirectoryOnly);
foreach (string file in sourceFiles)
{
using (Image sourceImage = Image.FromFile(file))
{
Console.WriteLine(sourceImage.Width);
Console.WriteLine(sourceImage.Height);
}
}
/*
Count lines with the changed names
*/
int correct = 0;
foreach (var s in linesA)
{
if (s.StartsWith("JeanShower"))
{
correct++;
}
System.Console.WriteLine(s);
string FullName = pathA + s;
Console.WriteLine(FullName);
}
Console.WriteLine("Named JeanShower is {0}", correct);
correct = correct + 1;
Console.WriteLine("Next Console number starts with, {0}", correct);
/* Check size of each picture and adjust */
DateTime startDateTime = DateTime.Now;
/* Changed name is */
foreach (var s in linesA)
{
if (!s.StartsWith("JeanShower"))
{
System.Console.WriteLine(s);
string OldName = pathA + s;
Console.WriteLine("OldName is {0}", OldName);
string NewName = pathA + "JeanShower" + correct + ".jpg";
Console.WriteLine("NewName is {0}", NewName);
Console.WriteLine();
File.Copy(OldName, NewName);
Console.WriteLine("New Console Created {0}", NewName);
File.Delete(OldName);
Console.WriteLine("Oldname has been removed, {0}", OldName);
correct++;
Console.WriteLine("");
}
else
{
System.Console.WriteLine(s);
string FullName = pathA + s;
Console.WriteLine(FullName);
Console.WriteLine("Already changed");
Console.WriteLine("");
}
}
int count = correct - 1;
Console.WriteLine("Number of pictures in directory is {0}", count);
Console.WriteLine("");
Console.WriteLine("Process is complete");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("HTML Code for index.html");
Console.WriteLine("");
Console.WriteLine("");
string picturename = "JeanShower";
string targetpathname = @"E:\Rename\JeanShower\index.html";
string webpath = "FirstChild/JeanShower/";
using (StreamWriter filelist1 = new StreamWriter(targetpathname, true))
{
int numpicture = 1;
do
{
filelist1.WriteLine("<div class=\"img" + numpicture + "\">");
filelist1.WriteLine("<a target=\"_blank\" href=\"" + picturename + numpicture + ".htm" + "\"" + ">");
filelist1.WriteLine("<img src=" + "\"" + picturename + numpicture + ".jpg" + "\"" + " alt=" + "\"" + picturename + numpicture + "\"" + " height=\"300\"" + " width=\"300\"" + "> </a>");
filelist1.WriteLine("<div class=\"" + "desc" + numpicture + "\"" + ">");
filelist1.WriteLine("<div class=" + "\"" + "h4bnomargin" + "\"" + ">");
filelist1.WriteLine("<a href=\"" + "../" + webpath + picturename + numpicture + ".jpg" + "\"" + "> Download" + " </a>");
filelist1.WriteLine("</div>" + " <br>");
filelist1.WriteLine(picturename + numpicture);
filelist1.WriteLine("</div>");
filelist1.WriteLine("</div>");
filelist1.WriteLine("");
numpicture++;
} while (numpicture <= count);
}
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("HTML Code for JeanShower.css");
Console.WriteLine("");
Console.WriteLine("");
using (StreamWriter filelist2 = new StreamWriter(@"E:\Rename\JeanShower\JeanShower.css", true))
{
int numpictureCSS = 1;
do
{
filelist2.WriteLine("div." + "img" + numpictureCSS + " { ");
filelist2.WriteLine(" margin: 1em; ");
filelist2.WriteLine(" padding: 0.5em; ");
filelist2.WriteLine(" border: 0.5em solid #0000ff; ");
filelist2.WriteLine(" height: auto; ");
filelist2.WriteLine(" width: auto; ");
filelist2.WriteLine(" float: left; ");
filelist2.WriteLine(" text-align: center; ");
filelist2.WriteLine("} ");
filelist2.WriteLine(" ");
filelist2.WriteLine("div." + "img" + numpictureCSS + " img { ");
filelist2.WriteLine(" display: inline; ");
filelist2.WriteLine(" margin: 0.5em; ");
filelist2.WriteLine(" border: 1px solid #ffffff; ");
filelist2.WriteLine("} ");
filelist2.WriteLine(" ");
filelist2.WriteLine("div." + "img" + numpictureCSS + " a:hover img { ");
filelist2.WriteLine(" border:1px solid #0000ff; ");
filelist2.WriteLine("} ");
filelist2.WriteLine(" ");
filelist2.WriteLine("div.desc" + numpictureCSS + " { ");
filelist2.WriteLine(" text-align: center; ");
filelist2.WriteLine(" font-weight: normal; ");
filelist2.WriteLine(" height: auto; ");
filelist2.WriteLine(" width: auto; ");
filelist2.WriteLine(" margin: 0.25em; ");
filelist2.WriteLine("} ");
filelist2.WriteLine(" ");
numpictureCSS++;
} while (numpictureCSS <= count);
}
Console.ReadKey();
}
}
}
这个程序允许我为我的网站创建HTML代码。我想读取每个jpg文件的高度和宽度。我在我的资源中添加了.NET 4。如何让image.height和image.width适用于每个JPG?一旦我得到图片的高度和宽度,我就可以为每张图片完成调整大小。我打算用正确的数字替换高度和宽度数字。
答案 0 :(得分:1)
这个怎么样:
using System;
using System.Drawing;
namespace JpegTest
{
class Program
{
static void Main(string[] args)
{
try
{
var image = new Bitmap(@"test.jpeg", true);
Console.WriteLine("Width: {0}", image.Width);
Console.WriteLine("Height: {0}", image.Height);
}
catch (ArgumentException)
{
Console.WriteLine("There was an error." + "Check the path to the image file.");
}
}
}
}
要测试:将一个名为test.jpeg的图片放入解决方案中,并将其Copy To Output Directory
属性设置为Copy Always
。