I'm tryng to export images from docx file.
How can I convert EncodedPackage from Shape object as image?
Sample of my code:
DocumentFormat.OpenXml.Vml.Shape shape = imageElement.Descendants<DocumentFormat.OpenXml.Vml.Shape>().FirstOrDefault();
byte[] bytes = System.Convert.FromBase64String(shape.EncodedPackage.Value.Replace("\n", ""));
System.Drawing.Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = System.Drawing.Image.FromStream(ms);
}
image.Save(fileName);
You can get the image from ImagePart. Try this:
var imageParts = doc.MainDocumentPart.ImageParts;
foreach (var imagePart in imageParts)
{
var uri = imagePart.Uri;
var filename = uri.ToString().Split('/').Last();
var stream = doc.Package.GetPart(uri).GetStream();
Bitmap b = new Bitmap(stream);
b.Save(#"C:\Extracted_Images\" + filename);
}
Related
i used PDFLIBNET to convert pdf to image :
public void ConvertPDFtoPNG(string filename, String dirOut)
{
try
{
PDFLibNet.PDFWrapper _pdfDoc = new PDFLibNet.PDFWrapper();
_pdfDoc.LoadPDF(filename);
System.Drawing.Image img = RenderPage(_pdfDoc, 0);
img.Save(Path.Combine(dirOut, Path.GetFileNameWithoutExtension(filename) + ".png"));
_pdfDoc.Dispose();
return;
}
catch
{
File.Copy(System.IO.Path.Combine(Environment.CurrentDirectory, "0.png"), Path.GetFileNameWithoutExtension(filename) + ".png");
}
}
This code is working properly
But i need to use in image handler without saving image
I change this code to use MemoryStream but get gray image :
public string ConvertPDFtoPNG(string filename)
{
PDFLibNet.PDFWrapper _pdfDoc = new PDFLibNet.PDFWrapper();
_pdfDoc.LoadPDF(filename);
System.Drawing.Image img = RenderPage(_pdfDoc, 0);
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
_pdfDoc.Dispose();
base64String = Convert.ToBase64String(ms.ToArray(), 0, ms.ToArray().Length);
}
please help me
thanks
Try this:
var b = File.ReadAllBytes(filename);
using (var ms = new MemoryStream(b))
{
var i = Image.FromStream(ms);
i.Save(ms, ImageFormat.Jpeg);
}
So my program is fixing some image artefacts, it goes like this:
void FixFile(string path)
{
var bmp = new WriteableBitmap(new BitmapImage(new Uri(path)));
bmp.Lock();
// magick
bmp.Unlock();
using (var stream = new FileStream(path.Replace("DSC", "fix_DSC"), FileMode.Create))
{
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
encoder.Save(stream);
}
}
The problem is that fixed image contains no EXIF data. How do I transfer EXIF data from the original image?
Load the source bitmap as BitmapFrame, not BitmapImage. Then pass the Metadata property of the source to the new BitmapFrame that is added to the Frames collection of the encoder.
public void FixFile(string path)
{
var source = BitmapFrame.Create(new Uri(path));
var metadata = (BitmapMetadata)source.Metadata;
var bmp = new WriteableBitmap(source);
bmp.Lock();
// magick
bmp.Unlock();
var target = BitmapFrame.Create(bmp, null, metadata, null); // here
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(target);
using (var stream = File.OpenWrite(path.Replace("DSC", "fix_DSC")))
{
encoder.Save(stream);
}
}
Hello friends i want to "convert a system.windows.control.image" to "System.Drawing.Image",but i am unable to do so.I am using below code for this
var e = (MouseButtonEventArgs)sender;
var device = e.MouseDevice.DirectlyOver;
System.Windows.Controls.Image img = (System.Windows.Controls.Image)device;
I have "img" i.e of type "system.windows.control.image" i need to convert it to bitmap or drawing type.
Try this....
Use your img variable in this code...
MemoryStream ms = new MemoryStream();
System.Windows.Media.Imaging.BmpBitmapEncoder bbe = new BmpBitmapEncoder();
bbe.Frames.Add(BitmapFrame.Create(new Uri(img.Source.ToString(),UriKind.RelativeOrAbsolute)));
bbe.Save(ms);
System.Drawing.Image img2 = System.Drawing.Image.FromStream(ms);
button1.Image = img2;
convert from byte[] to image
MemoryStream ms = new MemoryStream(imageByte);
Image image = Image.FromStream(ms);
it may help you..
I am trying to convert a image in my image folder and the image name defaultImage and update into my database table.
But now I am having problem in this line of code:
I change the code using this:
Image uploaded6 = Image.FromFile("/image/defaultImage.jpg");
instead of this:
System.Drawing.Image uploaded = System.Drawing.Image.FromStream(~/images/defaultImage);
I now getting the error of FileNotFoundException was unhandled by user code
I have tried this method using fileupload control and it working fine but not sure how to convert image in a folder.
How do I get the image from the folder in order to convert it using the method shown below.
Image uploaded6 = Image.FromFile("/image/defaultImage.jpg");
//System.Drawing.Image uploaded = System.Drawing.Image.FromStream();
System.Drawing.Image newImage = new Bitmap(1024, 768);
using (Graphics g = Graphics.FromImage(newImage))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(uploaded, 0, 0, 1024, 768);
}
byte[] results;
using (MemoryStream ms = new MemoryStream())
{
ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);
EncoderParameters jpegParms = new EncoderParameters(1);
jpegParms.Param[0] = new EncoderParameter(Encoder.Quality, 95L);
newImage.Save(ms, codec, jpegParms);
results = ms.ToArray();
}
string sqlImage = "update MemberReport set image1 = #Data where memberreportid = '" + Session["memberreportid"] + "'";
SqlCommand cmdImage = new SqlCommand(sqlImage);
cmdImage.Parameters.AddWithValue("#Data", results);
InsertUpdateData(cmdImage);
I guess your problem is with relative paths, instead of
Image uploaded6 = Image.FromFile("/image/defaultImage.jpg");
you should provide the local path, which you can get it this way:
Image uploaded6 = Image.FromFile(Server.MapPath("~/image/defaultImage.jpg"));
System.Drawing.Image.FromStream requires 'MemoryStream' as parameter.
byte[] file = null;
MemoryStream memoryStream = new MemoryStream();
memoryStream = new MemoryStream(file, false);
System.Drawing.Image objTempImg = System.Drawing.Image.FromStream(memoryStream)
byte[] file is based64 image
System.Drawing.Image.FromStream(Stream)
You must to send a Stream parameter in this method.
I'm using iconic.dll file to read data from compressed files (.zip file extensions)
Please check my code below
string zippath = txtFilePath.Text.Trim() + "\\" + foldername + ".zip";
ArrayList arrFiles = new ArrayList();
using (ZipFile zip = ZipFile.Read(enrollment))
{
foreach (ZipEntry e1 in zip)
{
arrFiles.Add(e1.ToString());
}
}
foreach (string path in arrFiles)
{
Image img1 = Image.FromFile(path); //geting error on this line
imageList.Images.Add(getThumbnaiImage(imageList.ImageSize.Width, img1));
}
How can I read image files from a compressed folder?
Try this :
using (ZipFile zip = ZipFile.Read(enrollment))
{
ZipEntry entry = zip["Image.bmp"];
entry.Extract(outputStream);
}
Also you can show your image in a pricturebox :
PictureBox pb = new PictureBox();
pb.Location = new Point(100, 100);
pb.SizeMode = PictureBoxSizeMode.Zoom;
var bmp = new Bitmap(outputStream);
pb.Image = bmp;
this.Controls.Add(pb);
This seems to be the solution:
using (ZipFile zip = ZipFile.Read(enrollment))
{
foreach (ZipEntry e1 in zip)
{
CrcCalculatorStream reader = e1.OpenReader();
MemoryStream memstream = new MemoryStream();
reader.CopyTo(memstream);
byte[] bytes = memstream.ToArray();
Image img1 = Image.FromStream(memstream);
imageList.Images.Add(getThumbnaiImage(imageList.ImageSize.Width, img1));
}
}