Click here to Skip to main content
Email Password   helpLost your password?

Introduction

Why this project? There are so many e-book readers already available, but none of them suited my requirements. They unzip pages on the disk, or hang between pages, or do not manage the library, etc.

Background

I decided to use this project to experiment more on WPF, and its graphic capacity fits the needs and helps fulfill the missing functionalities. It's more an experiment than anything else; the solution is clean but not perfect.

Functionalities

The application (in my opinion) has to fulfill the following:

BookReader1_new.jpg

Example: The main interface

BookReader2_new.jpg

Example: The options dialog

The Code

This project is very simple. As you can see in the class diagram below, it contains three forms and a few classes.

The Class Diagram

Click to enlarge image

The main class is Catalog. It holds a collection of IBooks (mapped to RarBook) which contains an IBookItem (mapped to RarPage). The catalog is in charge of parsing the book directory for all cbz or cbr files, and load/save itself (book name, bookmark, cover) in two separate binary files.

The loading algorithm is the following, and the main point is the usage of a thread to load the covers and parse the folder. The first load will be a bit long, but the generation of the covers will be threaded too. We will see the details later.

private void Load()
{
    try
    {
        string bin = 
          System.Reflection.Assembly.GetExecutingAssembly().
            Location.Replace(".exe", ".bin");
        if (File.Exists(bin))
        {
            //load the book name and bookmark
            if (LoadBooks(bin))
            {
                bin = 
                  System.Reflection.Assembly.GetExecutingAssembly().
                    Location.Replace(".exe", ".bin2");
                if (File.Exists(bin))
                {
                    // load the cover images
                    Thread t = new Thread(
                      new ParameterizedThreadStart(LoadCovers));
                    t.IsBackground = true;
                    t.Priority = ThreadPriority.BelowNormal;
                    t.Start(bin);
                }
            }

            //then refresh the book
            // ????
        }
        else //binary files does not exist, parse the directory
        {
            Thread t = new Thread(new 
                ParameterizedThreadStart(ParseDirectory));
            t.IsBackground = true;
            t.Priority = ThreadPriority.BelowNormal;
            t.Start(_bookPath);
        }
    }
    catch (Exception err)
    {
        ExceptionManagement.Manage(err, true);
    }
}

WPF brings the big advantage of advanced binding, and I use the ObservableCollection so I can notify later that some properties have changed, like the cover images which are loaded after the collection and the binding.

private ObservableCollection<ibook> _Books = 
        new ObservableCollection<ibook>();
public ObservableCollection<ibook> Books
{
  get { return _Books; }
  set { _Books = value; }
}

Once I load the book, I start a new thread to get the covers, and the UI will be updated with each Dispatcher=>Delegate - obligation, because the bitmap created here will be used on the UI thread.

public void LoadCovers(object fileName)
{
    IFormatter formatter = new BinaryFormatter();
    Stream streamBin = new FileStream((string)fileName,
        FileMode.Open,
        FileAccess.Read,
        FileShare.None);

    try
    {
        //book count
        int count = (int)formatter.Deserialize(streamBin);

        for (int i = 0; i < count; i++)
        {
            string filePath = (string)formatter.Deserialize(streamBin);

			MemoryStream coverStream = 
			  (MemoryStream)formatter.Deserialize(streamBin);

            foreach (IBook book in this._Books)
            {
                if (book.FilePath == filePath)
                {
                    MemoryStream stream2 = new MemoryStream();
                    coverStream.WriteTo(stream2);
                    coverStream.Flush();
                    coverStream.Close();

                    stream2.Position = 0;

                    Application.Current.Dispatcher.Invoke(
                       DispatcherPriority.Normal, (ThreadStart)delegate
                    {
                        BitmapImage myImage = new BitmapImage();
                        myImage.BeginInit();
                        myImage.StreamSource = stream2;
                        myImage.DecodePixelWidth = 70;
                        myImage.EndInit();

                        book.Cover = myImage;
                    });
                    coverStream = null;
                    stream2 = null;
                }
            }
        }
    }

When it's the first loading, I parse the directories, and construct the book with the second parameter set to true to generate its cover in a thread. See the constructor and the GenerateCover function.

foreach (FileInfo file in directory.GetFiles("*.*"))
                {
                    if (Properties.Settings.Default.BookFilter.Contains
						(file.Extension.ToUpper()))
                    {
                        if( !BookExist( file.FullName ) )
                            Application.Current.Dispatcher.Invoke
			    (DispatcherPriority.Background, (ThreadStart)delegate
                            {
                                IBook bk = (IBook)new RarBook(file.FullName, true);
                                bk.Size = file.Length;
                                Books.Add(bk);
                                this.IsChanged = true;
                            });
                    }
                }

The common functions of books are in the BaseBook class. The RarBook only contain specific code due to the wrapper used to read the files. Another important class is ImageCache which holds a collection of Bitmaps as specified in the options dialog (number and time). It's woken up by a timer in the main window.

private DispatcherTimer _TimerClock;

Debug Form

It will be displayed only if you activate the "debug checkbox" in the Options dialog. Otherwise, exceptions are logged into a ".log" file in the application directory. Always remember that everything that has something to do with the UI must be called on the same thread.

catch (Exception err)
{
    Application.Current.Dispatcher.Invoke(
       DispatcherPriority.Normal, (ThreadStart)delegate
    {
        ExceptionManagement.Manage(err, true);
    });
}

[or without thread]

catch (Exception err)
{
    ExceptionManagement.Manage(err, true);
}

Points of interest

Performances vs. Design

WPF is used as the rendering surface, but no special effects are used because it a pain in terms of performance... even more into a development VPC. I just use a simple theme from the WPF toolkit provided on CodePlex to skin the application. Then, I add threads for cover generation and binary reading.

The Problems

Serializing the Covers

I was unable to reload the saved catalog covers into the binary file. I don't know why but BitmapImage is not serializable... I found a solution by going through two MemoryStreams before creating the BitmapImage. This was the same with the rar stream...I got the message: "header corrupted".

Getting the Thread Working

That was the other major point because I put myself in a hurry when I saw the performance :-) but it works.

Contributors

Many thanks to SevenZip which allows me to uncompress in memory.

Conclusion

I hope you will enjoy it as much as I do. If you get any solution for the problems I faced, please forward...

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalsupport regular PDF text files?
puremood_2002
12:02 2 Feb '10  
Tried it, but it doesn't support regular PDF text files. Is there any suggestion on how to extend this application to view regular PDF file? thanks!
GeneralRe: support regular PDF text files?
chameau
22:35 2 Feb '10  
Hi,

No, this app is not reading PDF. I have made a function to convert PDF containing images through an external PDF reader.

I have no idea how to read PDF, but to stay in the same "philosophy", you have to write PDFBook and PDFPage classes...but pages are rendered as images...so have to "extract" PDF pages as images

Good luck...If you manage that, send it to me ! it will be a pleasure to include it either in for text or all PDF format.
GeneralProject is now hosted on Codeplex
chameau
21:36 1 Sep '09  
Please, visit http://wfpbookreader.codeplex.com/[^]
GeneralI did a comic reader too, please, take a look
quicoli
1:53 1 Jul '09  
Hi, take a look on it in this video:

http://video.msn.com/video.aspx?vid=45058284-ec36-413a-99f5-89a63a6a9f4c[^]
GeneralZero Length
CreF
22:50 30 Jun '09  
Hi Chameau,
the idea could be very interesting...Cool

The question is: why the source and the demo are empty? Confused

Tnks in advance
CreF Smile

the CreF

GeneralRe: Zero Length
chameau
23:44 30 Jun '09  
Sorry, it's corrected. the links were bads.Mad
GeneralDownload Files Are FUBAR
sam.hill
17:11 30 Jun '09  
Zero length files!

Question -- what format are the files? (Not the archives, but the book files?)
GeneralRe: Download Files Are FUBAR
thund3rstruck
17:27 30 Jun '09  
Thought it was just me getting 0 length files. I'm wondering if this app supports CHM e-books. I'm knee deep in development on a web based E-Book manager and I'm struggling with having to dynamically decompress CHM files to display them online.
GeneralRe: Download Files Are FUBAR
sam.hill
19:19 30 Jun '09  
Just noticed the subtitle: A WPF Book reader for cbz/cbr files
http://en.wikipedia.org/wiki/Comic_Book_Archive_file
GeneralRe: Download Files Are FUBAR
chameau
23:46 30 Jun '09  
Sorry, it's corrected. the links were bads.Mad


Last Updated 28 Nov 2009 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010