Solved

How to copy Clip of an Image to Clipboard C# Code in a WPF Application

Posted on 2011-09-23
1
758 Views
Last Modified: 2012-05-12
This is the Code the Works in Window Forms Application (Non-WPF)
"TiffImage" is a picturebox Control
public Bitmap ImageClip;

pivate void CopyBlock()
{
    Point topLeft = new Point(Math.Min(pointStart.X, pointEnd.X), Math.Min(pointStart.Y, pointEnd.Y));
    int clipWidth = Math.Abs(pointEnd.X - pointStart.X + 1),
        clipHeight = Math.Abs(pointEnd.Y - pointStart.Y + 1);

    ImageClip = new Bitmap(clipWidth, clipHeight);
    Graphics Gra = Graphics.FromImage(ImageClip);
    Gra.DrawImage(TiffImage.Image, new Rectangle(0, 0, clipWidth, clipHeight),
                                           topLeft.X, topLeft.Y, clipWidth, clipHeight, GraphicsUnit.Pixel);
    Gra.Dispose();

    Clipboard.SetDataObject(ImageClip);
}

What is the Code to do this in WFP Application
0
Comment
Question by:GEGAmbato
1 Comment
 

Accepted Solution

by:
GEGAmbato earned 0 total points
ID: 36595402
I have found the Solution to this problem.

To Clip a portion of an Image to the Clipboard you Crop the Image to a New Image.

            Image ClippedImage = new Image();
            ClippedImage.Width = clipWidth;

            CroppedBitmap cb = new CroppedBitmap((BitmapSource)imageLog.Source,
                                                  new Int32Rect( (int)Math.Round(topLeft.X),  (int)Math.Round(topLeft.Y),
                                                                  (int)Math.Round(clipWidth),  (int)Math.Round(clipHeight)));
            ClippedImage.Source = cb;
           

My problem was I was reading large Tiff File. 852.48 X 25957.44 was the test image size, one of the smaller ones.
The image DpiX = 200, DpiY = 200  Format = BlackWhite
PixelWidth = 1776 PixelHeight = 54078 BitsPerPixel = 1

To correct the problem of the cropped Image not matching what was selected I had to get the Image DpiX and DpiY  to 96.

This is the Code to Decode the Tiff File to, change Dpi to 96, and fit into an Image Control.

            Stream streamLog = new FileStream(pFile, FileMode.Open, FileAccess.Read, FileShare.Read);
            TiffBitmapDecoder decoder = new TiffBitmapDecoder(streamLog, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource logToBitMap = decoder.Frames[0];

            int pixWidth = logToBitMap.PixelWidth;
            int pixHeight = logToBitMap.PixelHeight;
            int bitPerPix = logToBitMap.Format.BitsPerPixel;
            PixelFormat pixFormat = logToBitMap.Format;

            int stride = pixWidth * bitPerPix;
            byte[] pixData = new byte[stride * pixHeight];
            logToBitMap.CopyPixels(pixData, stride, 0);

            BitmapSource bmpSource = BitmapSource.Create(pixWidth, pixHeight, 96, 96, pixFormat, null, pixData, stride);
            imageLog.Source = bmpSource;
            imageLog.Stretch = Stretch.Fill;

 Hope this help someone else, this took over a week to find a solution.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
itextsharp with c# 3 18
C# Service FileSystemwatcher isse 9 47
Making exceptions for ValidationAttribute 2 27
Telerik RadEditor Control Save 8 20
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question