dharmendr says:
Hi goplaa,
Refer the below article.
This code uses GetImageFormat method by using System.Reflection to convert string to ImageFormat. This code converts to variety of images from PDF file. It works like a charm.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Configuration;
using System.IO;
using System.Drawing.Imaging;
using PQScan.PDFToImage;
using System.Collections;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI;
using System.Reflection;
namespace PDFToImage
{
public partial class _Default : System.Web.UI.Page
{
public static ImageFormat GetImageFormat(string extension)
{
ImageFormat result = null;
PropertyInfo prop = typeof(ImageFormat).GetProperties().Where(p => p.Name.Equals(extension, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
if (prop != null)
{
result = prop.GetValue(prop,null) as ImageFormat;
}
return result;
}
protected void ddComapanyFilter_SelectedIndexChanged(object sender, EventArgs e)
{
var filesindirectory1 = Directory.GetFiles(Server.MapPath("/images/"), "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".jpg") || s.EndsWith(".pdf") || s.EndsWith(".png") || s.EndsWith(".bmp") || s.EndsWith(".gif"));
foreach (var file in filesindirectory1)
{
if(File.Exists(file)){
System.IO.File.Delete(file);
}
}
var ddlCountry = sender as DropDownList;
var ddlCity = ddlCountry.Parent.FindControl("ddlCountries") as DropDownList;
string conString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand("SELECT Name, Id, Data FROM RetPDF where Id=@id", con);
con.Open();
cmd.Parameters.AddWithValue("@id", ddlCity.SelectedValue);
using (var reader = cmd.ExecuteReader())
{
//int count = 1;
while (reader.Read())
{
byte[] imagedata = (byte[])reader["Data"];
FileStream fileStream = new FileStream(Server.MapPath("/images/Orderw.pdf"),
FileMode.CreateNew);
System.IO.File.WriteAllBytes(fileStream.ToString(), imagedata);
PDFDocument pdf = new PDFDocument();
// Create a file stream with PDF information
FileStream stream = new FileStream(fileStream.ToString(), FileMode.Open);
// Load PDF document from file stream.
pdf.LoadPDF(stream);
//if(pdf.PageCount !=-1)
pdf.DPI = 300;
// Set the width of output jpeg image.
int width = pdf.GetPageWidth(0) / 2;
// Set the height output jpeg image.
int height = pdf.GetPageHeight(0) / 2;
int counts = pdf.PageCount;
Bitmap bitmap;
for (int i = 0; i < counts; i++)
{
// Rasterize PDF page to image.
bitmap = pdf.ToImage(i);
// Save image to bmp file type.
string select = imageformat.SelectedValue;
string checkedval = "";
checkedval = imageformat.SelectedItem.Text;
bitmap.Save(Server.MapPath("/images/") + "output" + i + checkedval, GetImageFormat(select));
}
FileInfo[] files = new DirectoryInfo(Server.MapPath("/images/")).GetFiles();
List<String> images = new List<string>();
var exefiles = from FileInfo f in files
where f.Extension == ".jpg" || f.Extension == ".png" || f.Extension == ".bmp" || f.Extension == ".gif"
select f;
if (IsPostBack)
{
foreach (FileInfo f in exefiles)
{
images.Add(String.Format("~/Images/{0}", f));
Label1.Text = exefiles.Count().ToString() + " " + f.ToString();
}
}
RepeaterImages.DataSource = images;
RepeaterImages.DataBind();
stream.Close();
fileStream.Close();
files = null;
images.Clear();
}
RepeaterImages.DataSource = null;
RepeaterImages.DataSourceID = null;
con.Close();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand("SELECT Name, Id, Data FROM RetPDF");
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
ddlCountries.DataSource = ds;
ddlCountries.DataTextField = "Name";
ddlCountries.DataValueField = "Id";
ddlCountries.DataBind();
}
}
}
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PDFToImage._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body bgcolor="black" text="white">
<form id="form1" runat="server">
<center>
<h1><font size="5" font-family="Times New Roman"><u><i>Select the Image Formats To Convert PDF File</i></u></font></h1>
</center>
<div>
<asp:RadioButtonList ID="imageformat" runat="server" >
<asp:ListItem Text=".jpg" Value="Jpeg" />
<asp:ListItem Text=".bmp" Value="Bmp" />
<asp:ListItem Text=".gif" Value="Gif" />
<asp:ListItem Text=".png" Value="Png" />
</asp:RadioButtonList>
</div>
<div>
<asp:DropDownList ID="ddlCountries" AppendDataBoundItems="true" EnableViewState="true" AutoPostBack="True" runat="server" OnSelectedIndexChanged="ddComapanyFilter_SelectedIndexChanged"><asp:ListItem value="0">Please Select PDF Files</asp:ListItem>
</asp:DropDownList>
</div>
<div>
<asp:Repeater ID="RepeaterImages" runat="server" >
<ItemTemplate>
<asp:Image ID="Image1" height="800" width="650" runat="server" ImageURL='<%#Container.DataItem%>'/>
</ItemTemplate>
</asp:Repeater>
</div>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</form>
</body>
</html>