Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
908 views
in Technique[技术] by (71.8m points)

c# - Loading a picture file Image.FromFile VS FileStream

I must admit that I never understood what are the streams are all about- I always thought it's an internet thing. But now I run into a code that used a stream to load a file localy and I wonder if there is advantage for using a stream over... well the way I always loaded files:

private void loadingfromStream()
{
   DirectoryInfo dirInfo = new DirectoryInfo("c:/");
   FileInfo[] fileInfoArr = dirInfo.GetFiles();
   FileInfo fileInfo = fileInfoArr[0];       

   // creating a bitmap from a stream
   FileStream fileStream = fileInfo.OpenRead();            
   Bitmap bitmap = new Bitmap(fileStream);  
   Image currentPicture = (Image)bitmap       
}

vs.

private void loadingUsingImageClass
{    
   Image currentPicture = Image.FromFile(originalPath);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you know your code will be loading the data from a file, use Image.FromFile - it's obviously rather simpler code, and it's just possible that there are optimizations within the framework when it's dealing with files.

Using a stream is more flexible, but unless you need that flexibility, go with the file solution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...